Merge Two Maps in Golang

Sheeraz Gul Sep 28, 2022
  1. Use the Copy Method to Merge Two Maps in Golang
  2. User Defined Method to Merge Two Maps in Golang
Merge Two Maps in Golang

This tutorial demonstrates how to merge two maps in Golang.

Golang doesn’t provide any built-in functionality to merge the maps, but this can be achieved using the Copy method or following the corresponding process. Let’s try both methods.

Use the Copy Method to Merge Two Maps in Golang

By using the Copy method, we can copy the contents of one map to another map by which we can achieve the merge functionality for maps. The Copy method was introduced in Golang 1.18 version.

The Copy method takes two parameters, which are maps. The content of the second map will be copied into the first map.

Let’s try an example:

package main

import (
    "fmt"

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

func main() {
    map1 := map[string]int{
        "DelftstackOne":   10,
        "DelftstackTwo":   20,
        "DelftstackThree": 30,
    }
    map2 := map[string]int{
        "DelftstackTwo":   20,
        "DelftstackThree": 30,
        "DelftstackFour":  40,
        "DelftstackFive":  50,
    }
    // Print the map before merging
    fmt.Println("The First Map: ", map1)
    fmt.Println("The Second Map: ", map2)

    maps.Copy(map1, map2)
    fmt.Println("The merged Map is: ", map1)
}

The code above will merge two maps based on the union using the Copy method. The only disadvantage of the Copy method is that it modifies our first map.

See the output:

The First Map:  map[DelftstackOne:10 DelftstackThree:30 DelftstackTwo:20]
The Second Map:  map[DelftstackFive:50 DelftstackFour:40 DelftstackThree:30 DelftstackTwo:20]
The merged Map is:  map[DelftstackFive:50 DelftstackFour:40 DelftstackOne:10 DelftstackThree:30 DelftstackTwo:20]

Program exited.

User Defined Method to Merge Two Maps in Golang

We can design a method that will merge two maps based on a union, which will exclude the entry with the same value. Here is an example:

package main

import     "fmt"

type Delftstack struct {
    Id     int
    Name   string
    Salary int
}

type EmployeeMaps map[int]Delftstack

func MapUnion(map1, map2 EmployeeMaps) EmployeeMaps {
    for x, y := range map1 {
        if m, n := map2[x]; n {
            y.Salary += m.Salary
        }
        map2[x] = y

    }
    return map2
}

func main() {
    map1 := EmployeeMaps{2: {30, "Jack", 2000}, 5: {50, "Samuel", 1500}, 6: {60, "John", 1800}}
    map2 := EmployeeMaps{1: {10, "Pamela", 3000}, 2: {20, "Connor", 2000}, 3: {30, "Jack", 2000}, 4: {40, "Joe", 4000}}
    result := MapUnion(map2, map1)
    fmt.Println(result)

}

The code above merges two maps by excluding the entry with the same value, basically the union of maps. The code first creates a struct and maps with int keys and struct values; then, it uses the user-defined method MapUnion to merge them based on their union of values.

See the output:

map[1:{10 Pamela 3000} 2:{20 Connor 4000} 3:{30 Jack 2000} 4:{40 Joe 4000} 5:{50 Samuel 1500} 6:{60 John 1800}]

Program exited.
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Go Map