In the following code sample, the result is not what I would expect:
package main
import "fmt"
func main() {
src := map[int]int{1: 1, 2: 2, 3: 3}
fmt.Println("src ", src)
dst := make([]*int, 0, len(src))
for k, _ := range src {
dst = append(dst, &k)
}
for _, a := range dst {
fmt.Print(*a, " ")
}
fmt.Println()
}
Result:
src map[1:1 2:2 3:3]
3 3 3
Go Playground: https://play.golang.org/p/BSDsd3nojz
but I understand what is happening. The unchanging address of k
is being added to dst
, so when I loop over dst
, the same value is in every location: 3
.
The address of k
never changes in the loop, so the second loop keeps referring to that location, containing the last value it had, 3
.
How can I get the address of the current value of k
to be copied? Do I need something like this:
for k, _ := range src {
key = new(int)
*key = k
dst = append(dst, key)
}
That seems awkward.