Search code examples
dictionarygocopy

Copying all elements of a map into another


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).


Solution

  • 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.