Copying all elements of a map into another

Mike Samuel picture Mike Samuel · Sep 15, 2011 · Viewed 31.1k times · Source

Given

var dst, src map[K]V

I can copy all entries from src into dst by doing

for k, v := range src {
    dst[k] = v
}

Is there a more idiomatic way to do this?

copy only works on slices (and string as a source).

Answer

Lily Ballard picture Lily Ballard · Sep 15, 2011

That looks like a perfectly fine way to do this to me. I don't think copying one map into another is common enough to have a one-liner solution.