When do Go's pointers dereference themselves

前端 未结 1 403
孤街浪徒
孤街浪徒 2020-12-15 03:48

I just started diving into Go recently and I have one major point of confusion: I am struggling to understand when exactly it is necessary to dereference a pointer explicitl

相关标签:
1条回答
  • 2020-12-15 04:35

    The selector expression (e.g. x.f) does that:

    Selectors automatically dereference pointers to structs. If x is a pointer to a struct, x.y is shorthand for (*x).y; if the field y is also a pointer to a struct, x.y.z is shorthand for (*(*x).y).z, and so on. If x contains an anonymous field of type *A, where A is also a struct type, x.f is a shortcut for (*x.A).f.

    The definition of the indexing operation specifies that an array pointer may be indexed:

    For a of type A or *A where A is an array type, or for a of type S where S is a slice type

    0 讨论(0)
提交回复
热议问题