Home » How to create a copy of the map in Golang

How to create a copy of the map in Golang

Learn about How to create a copy of the map in Golang in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

How to create a copy of the map in Golang

// Golang program to create a copy of the map

package main

import "fmt"

func main() {
	var CountryCode = make(map[string]int)

	CountryCode["ind"] = 101
	CountryCode["aus"] = 102
	CountryCode["eng"] = 103
	CountryCode["pak"] = 104
	CountryCode["usa"] = 105

	var CloneMap = CountryCode

	fmt.Println("CountryCode: ", CountryCode)
	fmt.Println("CloneMap   : ", CloneMap)
}

In the main() function, we created a CountryCode map using make() function to store country code of specified country. Map store items in KEY/VALUE pair. Then we create a copy of the CountryCode map using the assignment operator and then print both maps

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
How to count the items of a map in Golang
How to delete an item from a map in Golang