Merge two maps in Golang
This article describes how to merge two maps in Golang.
Golang does not provide any in-built functionality to merge mappings, but this can be achieved using Copy method or by following the corresponding procedure. Let’s try both the methods.
Merge two maps using Copy method in Golang
By using the Copy method, we can copy the contents of one map to another map, thereby realizing the map merging function. The Copy method was introduced in Golang version 1.18.
The Copy method takes two parameters which are maps. The contents 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{
"JiyikOne": 10,
"JiyikTwo": 20,
"JiyikThree": 30,
}
map2 := map[string]int{
"JiyikTwo": 20,
"JiyikThree": 30,
"JiyikFour": 40,
"JiyikFive": 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 above code will use the Copy method to merge two maps based on union. The only disadvantage of the Copy method is that it modifies our first Map.
See the output:
The First Map: map[JiyikOne:10 JiyikThree:30 JiyikTwo:20]
The Second Map: map[JiyikFive:50 JiyikFour:40 JiyikThree:30 JiyikTwo:20]
The merged Map is: map[JiyikFive:50 JiyikFour:40 JiyikOne:10 JiyikThree:30 JiyikTwo:20]
User Defined Method to Merge Two Maps in Golang
We can devise a way to merge two maps based on a union, which will exclude entries with the same value. Here is an example:
package main
import "fmt"
type Jiyik struct {
Id int
Name string
Salary int
}
type EmployeeMaps map[int]Jiyik
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 above code merges two maps by excluding entries with the same value, basically a union of the maps. The code first creates a structure and maps with int keys and structure values; then, it uses the user-defined method MapUnion to merge them based on the union of the 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}]
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Install GoLang using Brew
Publish Date:2025/04/15 Views:82 Category:Go
-
This article describes how to install GoLang using Brew on Linux or macOS. Install GoLang using Brew brew installs missing packages in Linux and macOS. It makes it easy to install GoLang on Linux or macOS. Follow the steps below to install
GoLang RWMutex Detailed Introduction
Publish Date:2025/04/15 Views:116 Category:Go
-
This article introduces how to use rwmutex in Go language. Go language RWMutex mutex is the abbreviation of mutual exclusion, which is used to keep track of which thread has accessed a variable at any time. Mutex is a data structure provide
Enabling CORS in GoLang
Publish Date:2025/04/15 Views:102 Category:Go
-
This article describes how to enable and use CORS in GoLang. Go language CORS Cross-origin resource sharing (CORS) is a process based on HTTP headers that defines the origins from which browsers are allowed to load and use resources. CORS i
Function pointers in Go
Publish Date:2025/04/15 Views:83 Category:Go
-
A pointer in Go is a special-purpose variable that stores the memory address of other variables and the point where the memory is located. It can also access the value stored in that memory location. Pointer declaration in Go var pointer_na
Duration in Go
Publish Date:2025/04/15 Views:89 Category:Go
-
Go duration conversion can be done in a variety of ways. The time library and the time.duration method are often used to calculate and display time. Note that duration refers to the time elapsed between two defined time objects, as an int64
Create a new directory in Go
Publish Date:2025/04/15 Views:112 Category:Go
-
Go has many ways to create directories, one of which is os.Mkdir . We can assign specified names and permission bits to the newly formed directory. os.MkdirAll Useful for recursively creating multiple directories, including any missing pare
How to read a file into a string in GoLang
Publish Date:2025/04/15 Views:90 Category:Go
-
The Go language provides a lot of file manipulation tools, one of which is how to read files into strings. ioutil.ReadFile() , File.Read() , buf.ReadFrom() and strings.Builder are just a few of the methods that can be used to efficiently wr
Declaring constant Map in Go
Publish Date:2025/04/15 Views:166 Category:Go
-
Map is a collection of key-value pairs which can be sorted in any order. It assigns values to keys. The keys in a Map are always unique, but the values are not always unique. The map data structure is used for fast key-based dat
Getting a string representation of a structure in Go
Publish Date:2025/04/15 Views:63 Category:Go
-
Go allows us to serialize data from structures using a variety of simple standard methods. Converting a structure to a string using String method in Go The GoLang package String helps implement simple functions to manipulate and edit UTF-8