Getting the union of two maps in go

MapGoUnion

Map Problem Overview


I have a recursive function that creates objects representing file paths (the keys are paths and the values are info about the file). It's recursive as it's only meant to handle files, so if a directory is encountered, the function is recursively called on the directory.

All that being said, I'd like to do the equivalent of a set union on two maps (i.e. the "main" map updated with the values from the recursive call). Is there an idiomatic way to do this aside from iterating over one map and assigning each key, value in it to the same thing in the other map?

That is: given a,b are of type map [string] *SomeObject, and a and b are eventually populated, is there any way to update a with all the values in b?

Map Solutions


Solution 1 - Map

There is no built in way, nor any method in the standard packages to do such a merge.

The idomatic way is to simply iterate:

for k, v := range b {
    a[k] = v
}

Solution 2 - Map

If you have a couple of nested maps, left and right, then this function will recursively add the items from right into left. If the key is already in left then we recurse deeper into the structure and attempt only add keys to left (e.g. never replace them).


type m = map[string]interface{}

// Given two maps, recursively merge right into left, NEVER replacing any key that already exists in left
func mergeKeys(left, right m) m {
	for key, rightVal := range right {
		if leftVal, present := left[key]; present {
			//then we don't want to replace it - recurse
			left[key] = mergeKeys(leftVal.(m), rightVal.(m))
		} else {
			// key not in left so we can just shove it in
			left[key] = rightVal
		}
	}
	return left
}

NOTE: I do not handle the case in which the value is not itself a map[string]interface{}. So if you have left["x"] = 1 and right["x"] = 2 then the above code will panic when attempting leftVal.(m).

Solution 3 - Map

Since Go 1.18, you can simply use the Copy function from the golang.org/x/exp/maps package:

package main

import (
	"fmt"

	"golang.org/x/exp/maps"
)

func main() {
	src := map[string]int{
		"one": 1,
		"two": 2,
	}
	dst := map[string]int{
		"two":   2,
		"three": 3,
	}
	maps.Copy(dst, src)
	fmt.Println("src:", src)
	fmt.Println("dst:", dst)
}

(Playground)

Output:

src: map[one:1 two:2]
dst: map[one:1 three:3 two:2]

One caveat of this approach is that your maps' key type must be concrete. For instance, the compiler won't allow you to pass values of type map[io.Reader]int to the Copy function:

package main

import (
	"fmt"
	"io"

	"golang.org/x/exp/maps"
)

func main() {
	var src, dst map[io.Reader]int
	maps.Copy(dst, src)
	fmt.Println("src:", src)
	fmt.Println("dst:", dst)
}

(Playground)

Compiler output:

go: finding module for package golang.org/x/exp/maps
go: downloading golang.org/x/exp v0.0.0-20220328175248-053ad81199eb
./prog.go:12:11: io.Reader does not implement comparable

Go build failed.

More about this limitation in proposal: spec: permit values to have type "comparable".

Solution 4 - Map

Starting at go 1.18, thanks to the release of the Generics feature, there are now generic functions that union maps!

You can use a package like https://github.com/samber/lo in order to do so. Note that the key can be of any "comparable" type, while the value can be of any type.

Example:

package main

import (
	"fmt"
	"github.com/samber/lo"
)

func main() {
	map1 := map[string]interface{}{"k1": "v1", "k2": 2}
	map2 := map[string]interface{}{"k2": "v2new", "k3": true}
	map1 = lo.Assign(map1, map2)
	fmt.Printf("%v", map1)
}

The result is:

map[k1:v1 k2:v2new k3:true]

Solution 5 - Map

Go is limited by what type of map it is. I'd suspect that there isn't built in functions because of the infinite number of type declarations that could exist for a map. So you have to build your own Merge functions depending on what type of map you are using:

func MergeJSONMaps(maps ...map[string]interface{}) (result map[string]interface{}) {
	result = make(map[string]interface{})
	for _, m := range maps {
		for k, v := range m {
			result[k] = v
		}
	}
	return result
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjeffknuppView Question on Stackoverflow
Solution 1 - MapANisusView Answer on Stackoverflow
Solution 2 - MapJnBrymnView Answer on Stackoverflow
Solution 3 - Mapjub0bsView Answer on Stackoverflow
Solution 4 - MapAmit ItzkovitchView Answer on Stackoverflow
Solution 5 - MapChris GregoryView Answer on Stackoverflow