go - 为什么 fmt.Printf() 会在内存地址等其他包中记录 `const`?

标签 go memory-management

关闭。这个问题是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 // 10490880x100200 // AValue0x5 // 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 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).


对于 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
所以0x467269646179base 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/

相关文章:

android 在 View 删除时回收 View 中的所有位图

C++循环堆栈分配

go - 特定 URL 的 Hugo(go 静态站点生成器)列表

具有嵌套 map golang 的结构

vue.js - 如何在Go Web服务器和Vue.js前端之间交换数据? http发布:404

android - xml图像占用了太多内存 - android

Java 进程内存使用量远远超过 Runtime.totalMemory 等方法所告诉的

go - 如何将JSON响应中的特定数组值转换为只是数组中的第一个值?

go - 广播公司 : all goroutines are asleep - deadlock

c++ - 在现代 64 位系统上,什么会导致内存分配失败?