How to deep copy a map in Go?

To deep copy a map in Go, you can create a new map and copy each key-value pair from the original map to the new map. Here's an example code snippet:

package main import "fmt" func main() { // Original map originalMap := map[string]int{ "apple": 1, "banana": 2, "cherry": 3, } // Deep copy the map copiedMap := make(map[string]int) for key, value := range originalMap { copiedMap[key] = value } // Update the original map originalMap["apple"] = 5 // Print both maps fmt.Println("Original map:", originalMap) fmt.Println("Copied map:", copiedMap) }

In this example, we create an originalMap with some key-value pairs. Then, we create a new map called copiedMap using the make function. We iterate over each key-value pair in the original map and copy them to the new map.

After creating the copied map, we update the value for the "apple" key in the original map. Finally, we print both the original and copied maps to verify that the copied map remains unchanged.