关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。
想改进这个问题?将问题更新为 on-topic对于堆栈溢出。
1年前关闭。
Improve this question
fmt.Printf()
是怎么做到的?对待 %x?
当参数为 var
时或 const
.我从书上读到 const
没有Type
在指定之前,很难理解。
const AValue int32 = 1049088
func main() {
fmt.Printf("%#x\n", 1049088)
fmt.Printf("%#x\n", AValue)
fmt.Printf("%#x\n", int(time.Friday))
fmt.Printf("%#x\n", 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?
最佳答案
来自 docs :
Except when printed using the verbs %T and %p, special formatting considerations apply for operands that implement certain interfaces. In order of application:
...
5. If an operand implements methodString()
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).
对于
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
所以0x467269646179
是 base 16, lower-case, two characters per byte
time.Friday.String()
的输出.https://play.golang.org/p/avV-X2uiL1D
关于go - 为什么 fmt.Printf() 会在内存地址等其他包中记录 `const`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65445166/