On Thu, Jul 23, 2020 at 5:27 PM Robert Solomon <drrob...@gmail.com> wrote:
>
> Hi.  I'm going thru the example code for container/heap.  In the 
> documentation is this example:
>
> func (h *IntHeap) Pop() interface{} {
> old := *h
> n := len(old)
> x := old[n-1]
> *h = old[0 : n-1]   // my question is about this line.
> return x
> }
>
> Why does the last assignment have to assign to a dereferenced pointer, ie, *h 
> = old[0 : n-1]?
> Why does it not work if the last assignment is written as old = old[0 : n-1]? 
>  I know this does not work because I tried it.

I'm not quite sure how to answer this question because there seems to
be some sort of deep misunderstanding here.

"old" is a local variable in the IntHeap.Pop method.  Changing old, as
with "old = old[0 : n-1]", isn't going to affect the heap at all.  It
will just change a local variable that will go away when the method
returns.

Ian

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcV3s4fugC5rPhq39uDk1An0Ga79gUr9_BNFcJMMGOhoHA%40mail.gmail.com.

Reply via email to