呼叫具有可能的格式指令 [英] Call has possible formatting directive
本文介绍了呼叫具有可能的格式指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我运行这段代码时
package main
import ("fmt")
func main() {
i := 5
fmt.Println("Hello, playground %d",i)
}
(游乐场链接)
我收到以下警告:prog.go:5:Println调用具有可能的格式指令%d兽医退出了.
I get the following warning: prog.go:5: Println call has possible formatting directive %d Go vet exited.
执行此操作的正确方法是什么?
What is a proper way to do this?
推荐答案
fmt.Println
不会格式化%d
之类的格式.相反,它使用其参数的默认格式,并在参数之间添加空格.
fmt.Println
doesn't do formatting things like %d
. Instead, it uses the default format of its arguments, and adds spaces between them.
fmt.Println("Hello, playground",i) // Hello, playground 5
如果要使用printf样式格式,请使用 fmt.Printf
.
If you want printf style formatting, use fmt.Printf
.
fmt.Printf("Hello, playground %d\n",i)
您无需特别说明类型.%v
通常会弄清楚.
And you don't need to be particular about the type. %v
will generally figure it out.
fmt.Printf("Hello, playground %v\n",i)
这篇关于呼叫具有可能的格式指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文