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
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 fieldy
is also a pointer to a struct,x.y.z
is shorthand for(*(*x).y).z
, and so on. Ifx
contains an anonymous field of type*A
, whereA
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
whereA
is an array type, or for a of typeS
whereS
is a slice type