[Solved] Ranging over map keys of array type and slicing each array gives the same array for each iteration

[ad_1]

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.

[ad_2]

solved Ranging over map keys of array type and slicing each array gives the same array for each iteration