Learn to Clone or Copy Map in GO Like a PRO [5 Methods]


Written By - Tuan Nguyen

In the vast and versatile world of Go (Golang) programming, one often encounters the necessity to replicate data structures, ensuring that modifications in the replica do not affect the original. This necessity brings us to the concepts of copy map and clone map. When we talk about copying or cloning a map in Go, we refer to the creation of a duplicate map, possessing the same keys and values as the original. This duplication process is essential in various scenarios, such as preserving the original map’s state, preventing unauthorized modifications, or performing operations on the map without altering the actual data.

In this tutorial we will explore different methods which can be used to copy map or what we can also term as clone map in Golang.

 

Different Methods to Copy Map in GO

Here are various methods you can use to copy or clone a map in Go (Golang):

  1. Using Range Loop with Make: A simple method where a new map is initialized, and elements from the original map are iterated over and assigned to the new map, ensuring a separate memory allocation.
  2. Using JSON Marshalling and Unmarshalling: This technique involves converting the original map into a JSON object and then converting it back into a new map, ensuring that the new map is a separate entity.
  3. Using Deep Copy with Third-Party Packages: Utilizing available third-party packages that offer deep copy functionalities, ensuring that nested maps or maps with reference types are effectively cloned.
  4. Using Reflection: Employing the reflect package to dynamically create a new map and populate it with the elements of the original map, catering to various data types.
  5. Custom Implementation Based on Type: Crafting specific functions or methods tailored to the types stored in the map, ensuring an effective copying process tuned to the map's content.

 

Deep Copy vs Shallow Copy

When you copy map elements in Go, it’s essential to understand the difference between a deep copy and a shallow copy. The distinction lies in how the map's contents, particularly reference types like slices, maps, and pointers, are duplicated.

Deep Copy

A deep copy creates a new map with not only the immediate elements of the original map being duplicated but also the underlying elements of any reference types recursively copied. This means that modifications to the nested elements in the copied map won’t affect the original map.

Situations where Deep Copy is applicable:

  • When the map contains reference types like slices, maps, or pointers, and you want to manipulate them without affecting the original map.
  • When you need complete isolation between the original and copied map.

Example of Deep Copy:

package main

import (
	"fmt"
	"reflect"
)

func deepCopyMap(originalMap map[string]interface{}) map[string]interface{} {
	copiedMap := make(map[string]interface{})
	for key, value := range originalMap {
		copiedMap[key] = deepCopy(value)
	}
	return copiedMap
}

func deepCopy(item interface{}) interface{} {
	if item == nil {
		return nil
	}

	typ := reflect.TypeOf(item)
	val := reflect.ValueOf(item)

	if typ.Kind() == reflect.Ptr {
		newVal := reflect.New(typ.Elem())
		newVal.Elem().Set(reflect.ValueOf(deepCopy(val.Elem().Interface())))
		return newVal.Interface()
	} else if typ.Kind() == reflect.Map {
		newMap := reflect.MakeMap(typ)
		for _, k := range val.MapKeys() {
			newMap.SetMapIndex(k, reflect.ValueOf(deepCopy(val.MapIndex(k).Interface())))
		}
		return newMap.Interface()
	} else if typ.Kind() == reflect.Slice {
		newSlice := reflect.MakeSlice(typ, val.Len(), val.Cap())
		for i := 0; i < val.Len(); i++ {
			newSlice.Index(i).Set(reflect.ValueOf(deepCopy(val.Index(i).Interface())))
		}
		return newSlice.Interface()
	}

	return item
}

func main() {
	originalMap := map[string]interface{}{
		"number": 42,
		"list":   []int{1, 2, 3},
	}

	copiedMap := deepCopyMap(originalMap)

	// Modifying the copied map
	copiedList := copiedMap["list"].([]int)
	copiedList[0] = 99

	// Printing both maps to see the effect
	fmt.Println("Original Map:", originalMap) // Original Map: map[list:[1 2 3] number:42]
	fmt.Println("Copied Map:", copiedMap) // Copied Map: map[list:[99 2 3] number:42]
}

In this example, a deep copy of the map is made, and modifying the copied map does not affect the original map.

Shallow Copy

A shallow copy duplicates the immediate elements of the original map into a new map, but the reference types within the map still point to the same memory locations as those in the original map.

Situations where Shallow Copy is applicable:

  • When you only need to modify the top-level elements of the map and want to maintain links to the original nested structures.
  • When full isolation between the original and copied map is not necessary.

Example of Shallow Copy:

package main

import "fmt"

func shallowCopyMap(originalMap map[string][]int) map[string][]int {
	copiedMap := make(map[string][]int)
	for key, value := range originalMap {
		copiedMap[key] = value
	}
	return copiedMap
}

func main() {
	originalMap := map[string][]int{
		"numbers": {1, 2, 3},
	}

	copiedMap := shallowCopyMap(originalMap)

	// Modifying the copied map
	copiedMap["numbers"][0] = 99

	// Printing both maps to see the effect
	fmt.Println("Original Map:", originalMap) // Original Map: map[numbers:[99 2 3]]
	fmt.Println("Copied Map:", copiedMap) // Copied Map: map[numbers:[99 2 3]]
}

In this example, a shallow copy of the map is made. Modifying the copied map's slice also affects the original map because the slice's underlying array is still shared between both maps.

 

1. Using Range Loop with Make

When you want to copy map elements in Go, one straightforward approach is utilizing a range loop in conjunction with the make() function. Here’s how it works:

Performing Shallow Copy

In a shallow copy, the outermost objects are duplicated, while the inner elements retain references to the same memory locations as the original map’s inner elements.

package main

import "fmt"

func main() {
    originalMap := map[string]*int{
        "one":   new(int),
        "two":   new(int),
        "three": new(int),
    }
    *originalMap["one"] = 1
    *originalMap["two"] = 2
    *originalMap["three"] = 3

    // Shallow copy map
    copiedMap := make(map[string]*int)
    for key, value := range originalMap {
        copiedMap[key] = value
    }

    // Modifying the copied map
    *copiedMap["one"] = 100

    fmt.Println("Original Map:", *originalMap["one"]) // Output: 100
    fmt.Println("Copied Map:", *copiedMap["one"])     // Output: 100
}

Here, both the original and copied maps refer to the same memory locations of the inner elements. Modifying the copied map also modifies the original map.

Performing Deep Copy

A deep copy means cloning every item recursively, ensuring that no memory locations of the inner elements are shared between the original and copied map.

package main

import "fmt"

func main() {
    originalMap := map[string][]int{
        "one":   {1},
        "two":   {2},
        "three": {3},
    }

    // Deep copy map
    copiedMap := make(map[string][]int)
    for key, value := range originalMap {
        copiedValue := make([]int, len(value))
        copy(copiedValue, value)
        copiedMap[key] = copiedValue
    }

    // Modifying the copied map
    copiedMap["one"][0] = 100

    fmt.Println("Original Map:", originalMap["one"]) // Output: [1]
    fmt.Println("Copied Map:", copiedMap["one"])     // Output: [100]
}

Modifying the copied map doesn’t affect the original map because every element, including the inner slices, has been recursively cloned.

We can also declare the copyMap logic inside a function to make it re-usable:

func copyMap(originalMap map[string]int) map[string]int {
    copiedMap := make(map[string]int)
    for key, value := range originalMap {
        copiedMap[key] = value
    }
    return copiedMap // The copy logic is encapsulated and reusable.
}

func main() {
    originalMap := map[string]int{"one": 1, "two": 2}
    copiedMap := copyMap(originalMap)
}

 

2. Using JSON Marshalling and Unmarshalling

JSON Marshalling and Unmarshalling can also be used to copy map contents in Go. This method involves converting the map into a JSON string (marshalling) and then creating a new map by parsing the JSON string back (unmarshalling). This is more commonly used for deep copying and it is not so straight forward to perform shallow copy using this method.

By default, using JSON marshalling and unmarshalling performs a deep copy because the data is serialized and deserialized into a new memory allocation.

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	originalMap := map[string]interface{}{
		"key": map[string]int{"one": 1},
	}

	// Marshalling the map to JSON
	mapJSON, _ := json.Marshal(originalMap)

	// Unmarshalling JSON to a new map (Deep Copy)
	copiedMap := make(map[string]interface{})
	json.Unmarshal(mapJSON, &copiedMap)

	// Modifying the copied map
	// Additional type assertion is used here
	keyMap := copiedMap["key"].(map[string]interface{})
	keyMap["one"] = 100

	fmt.Println("Original Map:", originalMap) // Output: map[key:map[one:1]]
	fmt.Println("Copied Map:", copiedMap)     // Output: map[key:map[one:100]]
}

 

3. Using Third-Party Package (copier)

The copier package primarily focuses on struct copying but can also be utilized for maps, primarily resulting in deep copying behavior. Here's how you can use copier for copying maps:

package main

import (
	"fmt"
	"github.com/jinzhu/copier"
)

type ValueStruct struct {
	Value int
}

func main() {
	originalValue := ValueStruct{Value: 1}

	originalMap := map[string]*ValueStruct{
		"key": &originalValue,
	}

	// Making a copy using copier
	copiedMap := make(map[string]*ValueStruct)
	copier.Copy(&copiedMap, &originalMap)

	// Modifying the copied map
	copiedMap["key"].Value = 100

	fmt.Println("Original Value:", originalValue.Value) // Output: 1
	fmt.Println("Copied Map Value:", copiedMap["key"].Value) // Output: 100
}

In this example, even though pointers are used, copier creates a new instance of the ValueStruct, making the copiedMap independent of the originalMap. This is essentially a deep copy, as modifications to the copiedMap do not affect the originalMap.

The copier package might not directly support a shallow copy in terms of retaining the original pointers. For a shallow copy, you might need to use a different approach or package.

 

4. Using Reflection

Reflection in Go is a powerful tool that allows for inspection and modification of variable types and values at runtime. It’s more complex and might not be the most performance-efficient way of copying maps, but it offers flexibility. Here’s how you could employ reflection to accomplish both shallow and deep copy tasks:

Performing Shallow Copy

A shallow copy creates a new map, but the elements inside the map might still refer to the same memory locations as the original map.

package main

import (
	"fmt"
	"reflect"
)

func shallowCopyMap(src interface{}) interface{} {
	srcVal := reflect.ValueOf(src)
	dstVal := reflect.MakeMap(srcVal.Type())

	for _, key := range srcVal.MapKeys() {
		dstVal.SetMapIndex(key, srcVal.MapIndex(key))
	}

	return dstVal.Interface()
}

func main() {
	originalMap := map[string]*int{
		"one": new(int),
	}
	*originalMap["one"] = 1

	copiedMap := shallowCopyMap(originalMap).(map[string]*int)

	// Modifying the copied map
	*copiedMap["one"] = 100

	fmt.Println("Original Map:", *originalMap["one"]) // Output: 100
	fmt.Println("Copied Map:", *copiedMap["one"])     // Output: 100
}

Performing Deep Copy

A deep copy using reflection involves recursively copying each element, ensuring that the original and copied maps don’t share references.

package main

import (
	"fmt"
	"reflect"
)

func deepCopyMap(src interface{}) interface{} {
	srcVal := reflect.ValueOf(src)

	switch srcVal.Kind() {
	case reflect.Map:
		dstMap := reflect.MakeMap(srcVal.Type())
		for _, key := range srcVal.MapKeys() {
			dstMap.SetMapIndex(key, reflect.ValueOf(deepCopyMap(srcVal.MapIndex(key).Interface())))
		}
		return dstMap.Interface()

	case reflect.Slice:
		dstSlice := reflect.MakeSlice(srcVal.Type(), srcVal.Len(), srcVal.Cap())
		reflect.Copy(dstSlice, srcVal)
		for i := 0; i < srcVal.Len(); i++ {
			dstSlice.Index(i).Set(reflect.ValueOf(deepCopyMap(srcVal.Index(i).Interface())))
		}
		return dstSlice.Interface()

	default:
		return src
	}
}

func main() {
	originalMap := map[string][]int{
		"numbers": {1, 2, 3},
	}

	copiedMap := deepCopyMap(originalMap).(map[string][]int)

	// Modifying the copied map
	copiedMap["numbers"][0] = 99

	fmt.Println("Original Map:", originalMap["numbers"]) // Output: [1 2 3]
	fmt.Println("Copied Map:", copiedMap["numbers"])     // Output: [99 2 3]
}

In these examples, functions shallowCopyMap() and deepCopyMap() utilize reflection to perform the copy operations.

Note that using reflection might not be the most performant choice due to the dynamic type checking and recursive calls involved, especially in a deep copy. However, reflection provides a generic way to copy or clone maps, even when the exact types are not known at compile-time, offering a degree of flexibility and generality in handling various data structures.

 

5. Custom Implementation Based on Type

Creating a custom implementation based on types allows for a more targeted approach in copying maps. It gives you the flexibility to define the copying process, specifically according to the types and structures you are dealing with.

Performing Shallow Copy

Creating a shallow copy of a map based on specific types involves duplicating the outer map while keeping references to the same inner elements.

package main

import "fmt"

func shallowCopyIntMap(originalMap map[string]int) map[string]int {
	copiedMap := make(map[string]int)
	for key, value := range originalMap {
		copiedMap[key] = value
	}
	return copiedMap
}

func main() {
	originalMap := map[string]int{
		"one": 1,
		"two": 2,
	}

	copiedMap := shallowCopyIntMap(originalMap)

	// Modifying the copied map
	copiedMap["one"] = 100

	fmt.Println("Original Map:", originalMap) // Output: map[one:1 two:2]
	fmt.Println("Copied Map:", copiedMap)     // Output: map[one:100 two:2]
}

Performing Deep Copy

Creating a deep copy of a map based on specific types involves duplicating not only the outer map but also the inner elements, ensuring no shared references.

package main

import "fmt"

type InnerStruct struct {
	Value int
}

func deepCopyStructMap(originalMap map[string]*InnerStruct) map[string]*InnerStruct {
	copiedMap := make(map[string]*InnerStruct)
	for key, value := range originalMap {
		copiedMap[key] = &InnerStruct{Value: value.Value}
	}
	return copiedMap
}

func main() {
	originalMap := map[string]*InnerStruct{
		"one": &InnerStruct{Value: 1},
	}

	copiedMap := deepCopyStructMap(originalMap)

	// Modifying the copied map
	copiedMap["one"].Value = 100

	fmt.Println("Original Map Value:", originalMap["one"].Value) // Output: 1
	fmt.Println("Copied Map Value:", copiedMap["one"].Value)     // Output: 100
}

In these examples, shallowCopyIntMap and deepCopyStructMap functions are designed specifically based on the data types in the maps.

 

Frequently Asked Questions (FAQs)

What does it mean to "copy a map" in Go?

Copying a map in Go refers to creating a new map that contains all the keys and values of the original map. Depending on how you choose to copy the map, the new map might share references to the same underlying data (shallow copy) or have completely separate data (deep copy).

What is the difference between a "shallow copy" and a "deep copy" of a map in Go?

A shallow copy of a map involves creating a new map with new keys, but the values still hold references to the same objects as the original map. In a deep copy, a new map is created with new keys, and new objects are also created for the values, ensuring no shared references with the original map.

How do you perform a shallow copy of a map in Go?

A shallow copy can be performed using a simple loop to copy keys and values from the original map to a new one, for instance using the range loop. The new map gets filled with the keys and values of the original map, but the values (if they are reference types like slices or maps) still point to the same memory locations.

How do you perform a deep copy of a map in Go?

A deep copy can be achieved using various methods such as manually creating new objects for each value, utilizing the encoding/json package to marshal and unmarshal the map, or using third-party libraries that offer deep copying functionalities like copier.

When should I use a shallow copy, and when should I use a deep copy?

Use a shallow copy when it’s acceptable to have the copied map's values point to the same references as the original map’s values. Choose a deep copy when you want the copied map to be entirely independent of the original map, with no shared references, ensuring changes in the copied map do not affect the original map.

Is it possible to copy maps of custom structs in Go?

Yes, you can copy maps with custom structs as values. When copying, you can choose whether to perform a shallow copy, where the struct references are shared, or a deep copy, where new struct instances are created for the copied map.

Are there any third-party libraries that help in copying maps in Go?

Yes, there are third-party libraries, like copier or go-cmp, which provide functionalities to easily perform shallow and deep copies of maps and other data structures in Go. Before using a third-party library, ensure it is well-maintained and widely accepted by the community for reliability and performance.

Does Go have built-in support for deep copying maps?

Go doesn’t have a built-in function specifically for deep copying maps. However, you can achieve deep copying by using various techniques such as the encoding/json package for marshaling and unmarshaling maps, creating custom functions, or using third-party libraries.

 

Summary

In conclusion, copying or cloning maps in Go can be accomplished through various methods, each with its unique applications and considerations. The fundamental distinction lies between performing a shallow copy and a deep copy. A shallow copy is simpler and entails replicating the map's structure and entries, but not deeply duplicating nested reference types, leading to shared references. In contrast, a deep copy creates a completely independent copy of the map, ensuring that all nested elements, even those of reference types, are independently replicated without sharing memory references.

Different techniques, ranging from using simple range loops, JSON marshaling and unmarshaling, reflection, custom type-based implementations, to employing third-party packages, offer a spectrum of tools to achieve the desired level of copying. The selection of a suitable method is guided by various factors such as the necessity to maintain or sever reference linkages between the original and copied maps, performance considerations, and the specific data types and structures involved.

For more information and official documentation on maps in Go, refer to the following links:

 

Categories GO

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment