比如这么写:
fmt.Println("Hello, playground %d",i)
那么会出现warning:Println call has possible formatting directive %d Go vet exited.
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
If you want printf style formatting, use fmt.Printf.
fmt.Printf("Hello, playground %d\n",i)
And you don't need to be particular about the type. %v will generally figure it out.
fmt.Printf("Hello, playground %v\n",i)
Go打印格式详解
本文介绍了在Go语言中如何正确使用fmt.Println与fmt.Printf进行变量输出。对于%d等格式化指令,fmt.Println并不支持,它会以默认格式输出参数并在参数间插入空格。若要实现printf风格的格式化输出,则需使用fmt.Printf。此外,%v可以自动推断变量类型并进行格式化。
475

被折叠的 条评论
为什么被折叠?



