Copy a map

suggest change

In 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
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents