Essential Go Maps  Suggest an edit

Copy a map

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

  ↑ ↓ to navigate     ↵ to select     Esc to close