Is this syntax a pointer to slice or slice of pointers?

After looking at this blog post , I'm still unclear if the following syntax means fragment pointers or pointer to fragment.

foo  []*int

      

Which one and what is the general rule for this case?

+3


source to share


2 answers


This is a slice of pointers, just read from left to right: ( []

) slice ( *

) of pointers to ( int

) an integer. On the other hand, there *[]int

would be ( *

) a pointer to ( []

) a slice ( int

) of integers.



+8


source


This is a snippet of int pointers. []

the parentheses appear before the type, so *int

is the type in your example, but *[]int

is a pointer to an int array, or * [] * int for a pointer to an array of int pointers.



Whatever the style, I wouldn't use the syntax *[]int

in favor at all or something like foo := &[]int{}

that, which results in being foo

a pointer to the start of a new int array.

+2


source







All Articles