The slice which is created to be appended to b, points to &i, which (i) gets updated in next (last) loop iteration. Hence in last iteration, first element in `b` will start pointing to the new array [4,5,6].
This could have been mitigated had `i` been allocated to new location on every iteration. But that does not seem to be the case and I don't understand why. On a side note, I don't think this program would have compiler in Rust :) . On Wednesday, 23 May 2018 14:01:58 UTC+5:30, Sankar wrote: > > Thanks. > > 2018-05-23 13:55 GMT+05:30 <pierre...@gmail.com <javascript:>>: > >> You keep the reference of the loop index that points at the last array. >> Either make a copy of it or use a slice of arrays. >> >> https://play.golang.org/p/WcOHQ_wIKjx >> https://play.golang.org/p/CToFC9w88M7 >> >> >> Le mercredi 23 mai 2018 09:59:53 UTC+2, Sankar a écrit : >>> >>> 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. >>> >>> Thanks. >>> >>> >>> 2018-05-23 11:45 GMT+05:30 Sankar P <sankar.c...@gmail.com>: >>> >>>> Use an array instead of a slice. An array has a fixed size and can be >>>>> used as a key to a map >>>>> >>>>> https://play.golang.org/p/xxxmrwpx08A >>>>> >>>> >>>> This seem to not work. The arr is returning only duplicate elements in >>>> your playground url. >>>> >>>> For example: >>>> >>>> var arr [][]int >>>> for mem := range m { >>>> fmt.Println("Appending: ", mem[:]) >>>> arr = append(arr, mem[:]) >>>> } >>>> fmt.Println("Final arr is:", arr) >>>> >>>> the output is: >>>> >>>> Appending: [-1 0 1] >>>> Appending: [-1 -1 2] >>>> Final arr is: [[-1 -1 2] [-1 -1 2]] >>>> >>>> I am not really able to understand why the above code works so. The >>>> "Appending" and the "Final arr" statements have different values. >>>> >>>> -- >>>> Sankar P >>>> http://psankar.blogspot.com >>>> >>> >>> >>> >>> -- >>> Sankar P >>> http://psankar.blogspot.com >>> >> -- >> You received this message because you are subscribed to a topic in the >> Google Groups "golang-nuts" group. >> To unsubscribe from this topic, visit >> https://groups.google.com/d/topic/golang-nuts/fcBDb2wjtac/unsubscribe. >> To unsubscribe from this group and all its topics, send an email to >> golang-nuts...@googlegroups.com <javascript:>. >> >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > Sankar P > http://psankar.blogspot.com > -- 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.