1

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
Junya Kono
  • 1,121
  • 3
  • 10
  • 16

2 Answers2

5

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.
peterSO
  • 158,998
  • 31
  • 281
  • 276
1

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

nemo
  • 55,207
  • 13
  • 135
  • 135