Thanks Mathias for clearing this out.

I reasoned this out by looking at the address to which `i` was allocated to,
though I wanted to _peek_ inside the slice's header to be affirmative.  But
I can't clearly understand how to use unsafe pointer.  Thanks for showing
how to use it.

 One thing I don't understand is that why `i` is always allocated to same 
address?  Is it to reduce allocations?  Do other languages also do this?

On Wednesday, 23 May 2018 14:52:45 UTC+5:30, Matthias B. wrote:
>
> On Wed, 23 May 2018 13:29:17 +0530 
> Sankar P <sankar.c...@gmail.com <javascript:>> wrote: 
>
> > I extracted the confusing part alone, when appending to an array, the 
> > items are different than the resultant array into a separate go 
> > program. 
> > 
> > The Playground URL is: https://play.golang.org/p/BJM0H_rYNdb 
> > 
> > If someone can explain the output I would be thankful. It feels like 
> > a bug to me, but I am sure that I am only misunderstanding and it may 
> > not be a bug. 
>
> Maybe this makes it clearer: 
>
> https://play.golang.org/p/YolkDuYzrW8 
>
> There are 2 things to realize: 
>
> a) arrays are VALUES in Go, not pointers like in C. This means that the 
> loop variable i is not a pointer, it's an actual independent array that 
> has its own storage not shared with any of the arrays that are stored 
> as keys in the map. At the start of the first iteration of the map, the 
> numbers 1,2,3 are COPIED from the memory of the key [1,2,3] stored in 
> m1 to the memory of i. At the start of the 2nd iteration of the loop, 
> the numbers 4,5,6 are COPIED from the memory of the key [4,5,6] stored 
> in m1 to the memory of i. This overwrites the memory of i. 
>
> b) slices are a triples of a pointer, a length and maximum 
> length(capacity). Length and capacity are not relevant to understanding 
> this issue. So for the purposes of this issue, you can consider a slice 
> as a pointer. This means that the expression i[:] creates a pointer to 
> the storage address of i. Because i does not change its storage for the 
> whole duration of the loop, the expression i[:] is always the same. 
> It's a pointer to the storage that during the 1st iteration of the loop 
> contains 1,2,3 and during the 2nd iteration contains 4,5,6. 
> You can see this in my playground example. The printed pointer address 
> is the same in both cases. 
>
> Taken together this means that at the end of the program, b contains 2 
> copies of the same (address,length,capacity) triple with address 
> pointing to the storage of i (which because Go is garbage collected 
> remains valid). The storage of i contains the last value copied to it, 
> which is 4,5,6. 
>
> MSB 
>
> -- 
> If you do not fear death, how can you love life? 
>
>

-- 
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