If you want to combine two maps in Go, you can iterate over one of the maps and add each key-value pair to the other map. Here's an example function that takes two maps and returns the merged map:
main.go379 chars17 lines
In this example, we use the make()
function to create a new map result
. Then we iterate over m1
and add each key-value pair to result
using the result[k] = v
syntax. Finally, we iterate over m2
and add each key-value pair to result
, overwriting any keys that already exist in result
.
You can then call this function with two maps and get a new map that contains all key-value pairs from both maps:
main.go152 chars7 lines
gistlibby LogSnag