Junya Kono
Junya Kono

Reputation: 1271

How to expand variables with fmt.Println()

I can't expand variables with fmt.Println().

package main
import "fmt"
func main(){
  old := 20
  fmt.Println("I'm %g years old.",old)
}

result =>

I'm %g years old.
20

Upvotes: 1

Views: 4966

Answers (2)

peterSO
peterSO

Reputation: 166626

Use Printf not Println. Use %d for old which is type int. Add a newline.

For example,

package main

import "fmt"

func main() {
    old := 20
    fmt.Printf("I'm %d years old.\n", old)
}

Output:

I'm 20 years old.

Upvotes: 7

nemo
nemo

Reputation: 57659

As the documentation for fmt.Println states, this function does not support format specifiers. Use fmt.Printf instead.

Upvotes: 1

Related Questions