When trying to add int array keys of a map to a slice of int slices, ranging and using arr[:]
to slice array doesn\'t work as expected. The resultant slice contains
Since the map key type is an array, the assignment:
for k,_ := range ans {
will rewrite k
for every iteration. This will rewrite the contents of the array k
. The slice k[:]
points to k
as the underlying array, so all the slices with k
as their underlying array will be overwritten as well.
Copy the array for each iteration, as you did. That will create separate arrays for the slices you append.