I ran into this simple Golang code and was surprised by Go's behavior here. Can someone explain what is going on here, and how to write the below code correctly?
As you can see, I have a map
, where the key is an array
of int
. I add a couple of values and then I loop through the map
, convert each key to a slice
and append each key to an object of type [][]int
.
func test() {
myMap := make(map[[3]int]bool)
myMap[[3]int{1, 2, 3}] = true
myMap[[3]int{0, 5, 4}] = true
myMap[[3]int{9, 7, 1}] = true
myMap[[3]int{0, 2, 8}] = true
array := [][]int{}
for val := range myMap {
array = append(array, val[:])
}
fmt.Println(array)
}
I was expecting the last line to print [[1,2,3], [0,5,4], [9,7,1], [0,2,8]]
, however, to my surprise it prints [[0 2 8] [0 2 8] [0 2 8] [0 2 8]]
, or [[9 7 1] [9 7 1] [9 7 1] [9 7 1]]
, or some other variation containing only one of the keys multiple times.
My go version is 1.16.5