当我运行这段代码时
package main
import ("fmt")
func main() {
i := 5
fmt.Println("Hello, playground %d",i)
}
( playground link )
我收到以下警告:
prog.go:5: Println call has possible formatting directive %d
Go vet exited.
执行此操作的正确方法是什么?
最佳答案
fmt.Println
不会像 %d
这样格式化。相反,它使用其参数的默认格式,并在它们之间添加空格。
fmt.Println("Hello, playground",i) // Hello, playground 5
如果你想要 printf 风格的格式化,使用 fmt.Printf
.
fmt.Printf("Hello, playground %d\n",i)
而且您不需要特别注意类型。 %v
通常会弄清楚。
fmt.Printf("Hello, playground %v\n",i)
关于go - 调用有可能的格式化指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53961617/