Copy a map
suggest changeIn Go, every assignment copies the value.
A map value is a small header that points at the buckets. Assigning a map, or passing it to a function, copies that header. Both variables share the same buckets, so a write through one is visible through the other.
Copy the entries when you need an independent map:
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