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 <javascript:>>: > >> 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 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.