There are cases when using a pointer to a slice is both natural and
correct. For example, when using the container/heap package, I often write
code similar to

type Heap []Item
func (h *Heap) Pop() interface{} { ... }
func (h *Heap) Push(x interface{}) { ... }

Here, "func (h Heap) ..." would be incorrect, since these functions need to
modify the length of the slice, not just the contents.

Writing (*h)[i] instead of h[i] inside these functions is an irritation,
but just a mild one.

On Sat, Dec 3, 2016 at 7:10 PM, Rob Pike <r...@golang.org> wrote:

> A pointer to an array is useful. A pointer to a slice is much less so; in
> fact, it's often a mistake. The slice already contains a pointer to the
> data (in fact, as a pointer to an array!). Best not to promote the bad idea.
>
> -rob
>
>
> On Sun, Dec 4, 2016 at 11:27 AM, Patrick Smith <pat42sm...@gmail.com>
> wrote:
>
>> The spec says:
>>
>> "A primary expression of the form
>>
>> a[x]
>>
>> denotes the element of the array, pointer to array, slice, string or map
>> a indexed by x."
>>
>> I am curious about one point - what is the reason for allowing a to be a
>> pointer to array, but not allowing a to be a pointer to slice? In both
>> cases, a[x] would be equivalent to (*a)[x].
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to golang-nuts+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to