Copy a map
suggest changeIn Go all values are passed by copy.
However, similar to slices, the value of a map is only a reference to underlying data.
When you assign a map to another variable or pass to another function, you only copy the reference.
To copy the values, we need to write a bit of code:
src := make(map[string]int)
src["one"] = 1
src["two"] = 2
dst := make(map[string]int)
for key, value := range src {
dst[key] = value
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents