为什么不使用%v打印int和字符串 [英] Why not use %v to print int and string

查看:61
本文介绍了为什么不使用%v打印int和字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道要打印int,我们可以使用%d,而string我们可以使用%s 但是我们仍然可以使用%v进行打印.那么,如果我始终使用%v进行打印怎么办?如果这样做,会发生什么问题?

解决方案

没什么不好的,但是%d动词指示 fmt 是一个数字(以10为底),而%v动词表示使用可以覆盖的 default 格式.

请参见以下示例:

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)
}

输出(在转到游乐场上尝试):

2 *2*

使用%v动词时,fmt程序包检查值是否实现 接口(它是单个String() string方法),如果是,则将调用该方法将值转换为string(如果指定了标志,则可以进一步格式化).

格式规则的完整列表在fmt的软件包文档中,并引用了相关部分:

除了使用动词%T和%p进行打印时,特殊的格式注意事项适用于实现某些接口的操作数.按申请顺序:

  1. 如果操作数是reflect.Value,则将操作数替换为其所保存的具体值,并使用下一条规则继续打印.

  2. 如果操作数实现Formatter接口,则将调用它.格式化程序可以很好地控制格式化.

  3. 如果%v动词与#标志(%#v)一起使用,并且操作数实现GoStringer接口,则会被调用.

如果该格式(对于Println等而言是隐含的%v)对于字符串(%s%q%v%x%X)有效,则以下两个规则适用:

  1. 如果操作数实现错误接口,则将调用Error方法将对象转换为字符串,然后根据动词的要求对其进行格式化(如果有).

  2. 如果操作数实现String()字符串方法,则将调用该方法将对象转换为字符串,然后将根据动词的要求对其进行格式化(如果有).

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?

解决方案

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:

  1. 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).

  2. 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).

这篇关于为什么不使用%v打印int和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆