为什么FMT.Prinft() 在其他包中记录"const",例如内存地址?



当参数是varconst时,fmt.Printf()如何处理%x?。我从书中读到const没有指定之前Type,很难理解。

const AValue int32 = 1049088
func main() {
fmt.Printf("%#xn", 1049088)
fmt.Printf("%#xn", AValue)
fmt.Printf("%#xn", int(time.Friday))
fmt.Printf("%#xn", time.Friday)
}

原木:

0x100200 // 1049088
0x100200 // AValue
0x5 // int(time.Friday)
0x467269646179 // time.Friday is a type Weekday int


Is `0x467269646179` some kind address of time.Friday?

从文档中:

除非使用动词 %T 和 %p 打印,否则特殊格式 注意事项适用于实现某些接口的操作数。 按应用顺序:
...
5。如果操作数实现String()字符串的方法,则将调用该方法将对象转换为字符串,然后 根据谓词(如果有)的要求设置格式

对于string谓词格式定义为:

%s  the uninterpreted bytes of the string or slice
%q  a double-quoted string safely escaped with Go syntax
%x  base 16, lower-case, two characters per byte
%X  base 16, upper-case, two characters per byte

所以0x467269646179time.Friday.String()输出的base 16, lower-case, two characters per byte.

https://play.golang.org/p/avV-X2uiL1D

最新更新