Golang Program to Create a Copy of the Map


Maps are reference data types in Golang, which means that assigning one map variable to another creates a reference to the same underlying data structure. To create an independent copy of a map, we need to use different methods. In this article, we will explore two methods that include the manual method and the copy() function method to create a copy of map in golanguage.

Syntax

newMap := make(map[keyType]valueType)

In this syntax, we first declare a new map named newMap using the make() function, specifying the appropriate key and value types.

originalMap := map[string]int

The syntax originalMap := map[string]int{} declares and initialises a new map named "originalMap" with keys of type string and values of type int. This is an empty map, which we will later populate or use to make a copy of another map.

newMap := make(map[string]int, len(originalMap))

the syntax newMap := make(map[string]int, len(originalMap)) creates a new map named "newMap" with keys of type string and values of type int.

func copyMap(newMap, originalMap map[string]int) 

The syntax func copyMap(newMap, originalMap map[string]int) defines a function named copyMap. This function takes two parameters: newMap and originalMap, both of which are maps with keys of type string and values of type int.

Algorithm

  • Create a new map variable with the same key and value types as the original map.

  • Iterate over the key−value pairs in the original map.

  • For each iteration, copy the key−value pair to the new map.

  • The new map is now a separate copy of the original map.

Example

In this example, consider an original map originalMap with key−value pairs representing student names and their corresponding ages. We will create a copy of this map using the manual method.

package main

import "fmt"

func main() {
	originalMap := map[string]int{
		"John":  20,
		"Alice": 25,
		"Bob":   22,
	}

	newMap := make(map[string]int)
	for key, value := range originalMap {
		newMap[key] = value
	}

	fmt.Println("Original Map:", originalMap)
	fmt.Println("Copied Map:", newMap)
}

Output

Original Map: map[Alice:25 Bob:22 John:20]
Copied Map: map[Alice:25 Bob:22 John:20]

Example

In this example we are going to create a copy of map in golanguage using a combination of conversion and iteration. Let us Consider an original map originalMap with key−value pairs representing student names and their corresponding ages. We will create a copy of this map using the copy() function.

package main

import "fmt"

func main() {
	originalMap := map[string]int{
		"John":  20,
		"Alice": 25,
		"Bob":   22,
	}

	newMap := make(map[string]int, len(originalMap))
	copyMap(newMap, originalMap)

	fmt.Println("Original Map:", originalMap)
	fmt.Println("Copied Map:", newMap)
}

func copyMap(newMap, originalMap map[string]int) {
	for key, value := range originalMap {
		newMap[key] = value
	}
}

Output

Original Map: map[Alice:25 Bob:22 John:20]
Copied Map: map[Alice:25 Bob:22 John:20]

Example

In this example, let us consider a map named originalMap that contains key−value pairs representing student names and their corresponding scores. We want to create a copy of map in golanguage named newMap. we iterate over the key−value pairs of the original map and create a new map by assigning the same key−value pairs to the new map.

package main

import "fmt"

func main() {
	originalMap := map[string]int{
		"John":   85,
		"Alice":  90,
		"Bob":    75,
		"Sara":   95,
		"Michael": 80,
	}

	// Creating a copy of the map using a loop
	newMap := make(map[string]int)
	for key, value := range originalMap {
		newMap[key] = value
	}

	fmt.Println("Original Map:", originalMap)
	fmt.Println("New Map:", newMap)
}

Output

Original Map: map[Alice:90 Bob:75 John:85 Michael:80 Sara:95]
New Map: map[Alice:90 Bob:75 John:85 Michael:80 Sara:95]

Real Life Implementation

Inventory Management

Imagine you're managing inventory for a retail store. You have a map that contains the products in stock along with their quantities. You want to generate reports for different departments without altering the original stock data. Creating a copy of the stock map allows you to generate reports for various departments without modifying the original stock levels. This ensures accurate reporting for each department while preserving the integrity of the stock data.

Survey Data Analysis

Suppose you're analyzing survey responses for a research project. You have a map that stores the survey questions as keys and the corresponding responses as values. You want to perform different analyses on the data without changing the original responses. By creating copies of the response map, you can run various analyses and transformations on the data without altering the original survey responses. This enables you to experiment with different analysis techniques while keeping the raw data intact.

Conclusion

Map copying involves the iterating over the old maps key value pair and transferring them to a new map. In this article we have looked at how we can create a copy of map in golanguage using a manual method, the copy() function, or a loop to iterate and assign key−value pairs. Understanding these methods provides valuable insights into effectively managing map data in Go programming. These methods can be used in inventory management as well as survey data analysis .

Updated on: 07-Sep-2023

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements