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

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

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失