doutan6286
2017-09-23 00:46 阅读 24
已采纳

为什么在打印指针时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 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).

    点赞 评论 复制链接分享

相关推荐