当我运行这个代码时
package main
import ("fmt")
func main() {
i := 5
fmt.Println("Hello, playground %d",i)
}
我收到以下警告:
prog.go:5: Println call has possible formatting directive %d
Go vet exited.
做这件事的合适方式是什么?
当我运行这个代码时
package main
import ("fmt")
func main() {
i := 5
fmt.Println("Hello, playground %d",i)
}
我收到以下警告:
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)