doutan6286 2017-09-23 00:46 采纳率: 100%
浏览 50
已采纳

为什么在打印指针时fmt.Println不一致?

I'm an experienced programmer but have never before touched Go in my life.

I just started playing around with it and I found that fmt.Println() will actually print the values of pointers prefixed by &, which is neat.

However, it doesn't do this with all types. I'm pretty sure it is because the types it does not work with are primitives (or at least, Java would call them that, does Go?).

Does anyone know why this inconsistent behaviour exists in the Go fmt library? I can easily retrieve the value by using *p, but for some reason Println doesn't do this.

Example:

package main

import "fmt"

type X struct {
    S string
}

func main() {
    x := X{"Hello World"}
    fmt.Println(&x)     // &{Hello World} <-- displays the pointed-to value prefixed with &
    fmt.Println(*(&x))  // {Hello World}

    i := int(1)
    fmt.Println(&i)     // 0x10410028     <-- instead of &1 ?
    fmt.Println(*(&i))  // 1
}
  • 写回答

1条回答 默认 最新

  • douju1968 2017-09-23 02:53
    关注

    The "technical" answer to your question can be found here:

    https://golang.org/src/fmt/print.go?#L839

    As you can see, when printing pointers to Array, Slice, Struct or Map types, the special rule of printing "&" + value applies, but in all other cases the address is printed.

    As for why they decided to only apply the rule for those, it seems the authors considered that for "compound" objects you'd be interested in always seeing the values (even when using a pointer), but for other simple values this was not the case.

    You can see that reasoning here, where they added the rule for the Map type which was not there before:

    https://github.com/golang/go/commit/a0c5adc35cbfe071786b6115d63abc7ad90578a9#diff-ebda2980233a5fb8194307ce437dd60a

    I would guess this had to do with the fact that it is very common to use for example pointers to Struct to pass them around (so many times you'd just forget to de-reference the pointer when wanting to print the value), but no so common to use pointers to int or string to pass those around (so if you were printing the pointer you were probably interested in seeing the actual address).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能