Re: [go-nuts] Append array to slice of slice behavior puzzles me

2018-11-21 Thread Sam Mortimer
On Wednesday, November 21, 2018 at 2:38:42 PM UTC-8, Sun Frank wrote: > > Perfect! Thank you so much for your answer and explanation, : ) > It is, unfortunately, a very common "gotcha". More discussion can be found here: https://github.com/golang/go/issues/20733 Cheers, -Sam. > Best

Re: [go-nuts] Append array to slice of slice behavior puzzles me

2018-11-21 Thread Sun Frank
Perfect! Thank you so much for your answer and explanation, : ) Best Regards Mail : elitegobli...@gmail.com Mobile : 0422 118 452 TechBlog : http://elitegoblin.github.io GitHub: http://github.com/eliteGoblin/ andrey mirtchovski 于2018年11月22日周四 上午9:22写道: > this is a case of a

Re: [go-nuts] Append array to slice of slice behavior puzzles me

2018-11-21 Thread andrey mirtchovski
this is a case of a variable being reused during a range loop. essentially k[:] is optimized to something resembling (a pointer to the memory location of the backing array) which is appended as such to the [][]int variable. the next iteration it is the same variable in memory, however it's

Re: [go-nuts] Append array to slice of slice behavior puzzles me

2018-11-21 Thread Miguel Angel Rivera Notararigo
Hi, just add k := k after the for-range line -- 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

[go-nuts] Append array to slice of slice behavior puzzles me

2018-11-21 Thread Sun Frank
Hi guys, I came across some code that surprised me, but I could not figured out why it behave like this, s := make([][]int, 0) a1 := [3]int{1, 2, 3} a2 := [3]int{4, 5, 6} s = append(s, a1[:]) s = append(s, a2[:]) fmt.Println(s) mp := make(map[[3]int]bool) mp[[3]int{1, 2, 3}] = true mp[[3]int{4,