英文:
Why not use %v to print int and string
问题
我知道打印int
可以使用%d
,而打印string
可以使用%s
,但我们仍然可以使用%v
来打印它们。那么,如果我总是使用%v
来打印它们,会出现什么问题?
英文:
I know to print int
we can use %d
, and string
we can use %s
but we can still use %v
to print them. So what if I always use %v
to print them? What issue will happen if I do this?
答案1
得分: 86
%d
是一个格式化动词,它告诉 fmt
包将其作为一个数字(使用十进制)打印出来,而 %v
则表示使用“默认”格式,可以被覆盖。
看下面的例子:
type MyInt int
func (mi MyInt) String() string {
return fmt.Sprint("*", int(mi), "*")
}
func main() {
var mi MyInt = 2
fmt.Printf("%d %v", mi, mi)
}
输出结果(在 Go Playground 上尝试):
2 *2*
当使用 %v
动词时,fmt
包会检查值是否实现了 fmt.Stringer
接口(一个 String() string
方法),如果是,则调用该方法将值转换为 string
(如果指定了标志,则可以进一步格式化)。
完整的格式化规则列表在 fmt
包的文档中,引用相关部分:
> 除非使用 %T 和 %p 动词打印,否则对实现了某些接口的操作数会应用特殊的格式化规则。应用顺序如下:
>
> 1. 如果操作数是 reflect.Value,则操作数将被其所持有的具体值替换,并继续打印下一个规则。
>
> 2. 如果操作数实现了 Formatter 接口,则将调用它。Formatter 提供了对格式化的精细控制。
>
> 3. 如果使用了 # 标志(%#v)并且操作数实现了 GoStringer 接口,则将调用该接口。
>
> 如果格式(对于 Println 等隐式为 %v)对于字符串有效(%s %q %v %x %X),则适用以下两个规则:
>
> 4. 如果操作数实现了 error 接口,则将调用 Error 方法将对象转换为字符串,然后按照动词(如果有)所需的格式进行格式化。
>
> 5. 如果操作数实现了 String() string 方法,则将调用该方法将对象转换为字符串,然后按照动词(如果有)所需的格式进行格式化。
英文:
Nothing bad will happen, but the %d
verb instructs the fmt
package to print is as a number (using base 10), and the %v
verb means to use the default format which can be overridden.
See this example:
type MyInt int
func (mi MyInt) String() string {
return fmt.Sprint("*", int(mi), "*")
}
func main() {
var mi MyInt = 2
fmt.Printf("%d %v", mi, mi)
}
Output (try it on the Go Playground):
2 *2*
When using the %v
verb, the fmt
package checks if the value implements the fmt.Stringer
interface (which is a single String() string
method), and if so, that method will be called to convert the value to string
(which may be formatted further if flags are specified).
The complete list of formatting rules is in the package doc of fmt
, quoting the relevant part:
> Except when printed using the verbs %T and %p, special formatting considerations apply for operands that implement certain interfaces. In order of application:
>
> 1. If the operand is a reflect.Value, the operand is replaced by the concrete value that it holds, and printing continues with the next rule.
>
> 2. If an operand implements the Formatter interface, it will be invoked. Formatter provides fine control of formatting.
>
> 3. If the %v verb is used with the # flag (%#v) and the operand implements the GoStringer interface, that will be invoked.
>
> If the format (which is implicitly %v for Println etc.) is valid for a string (%s %q %v %x %X), the following two rules apply:
>
> 4. If an operand implements the error interface, the Error method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).
>
> 5. If an operand implements method String() string, that method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论