After looking at this blog post, I am still unclear about whether the following syntax means a slice of pointers or a pointer to a slice.
foo []*int
Which one is it, and what is the general rule of thumb for this case?
This is a slice of pointers, just read from left to right: ([]
) a slice of (*
) pointers to (int
) integer. On the other hand, *[]int
would be (*
) a pointer to ([]
) a slice of (int
) integers.
That is a slice of int pointers. []
brackets come before the type so *int
is the type in your example while *[]int
is a pointer to an int array or *[]*int for a pointer to an array of int pointers.
Just as a matter of style I would generally avoid using *[]int
syntax in favor or something like foo := &[]int{}
which results in foo
being a pointer to the start of a new int array.