"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Deep copying of Go language maps and clearing the original map methods

Deep copying of Go language maps and clearing the original map methods

Posted on 2025-04-19
Browse:983

How to Deep Copy a Go Map and Clear the Original Without Affecting the Copy?

Creating an Associative Map in Go: How to Deep Copy and Clear the Original

In Go, you're attempting to copy the contents of a map called aMap into another map named aSuperMap. Subsequently, you want to clear aMap to accept new values in the next loop iteration. However, you're facing the issue of clearing the map without also clearing its reference in aSuperMap.

The Problem with References

Your pseudo code illustrates that when you delete an item from aMap using delete(aMap, x), the reference to that item is also removed from aSuperMap. This is because aSuperMap[y] holds a reference to aMap, and when you modify aMap (in this case, by deleting an item), you're actually modifying the underlying data that aSuperMap points to.

Deep Copying Maps

To truly create an associative map in Go, you need to perform a deep copy of the map. This involves creating a new map object with its own copy of the data, rather than simply holding a reference to the original map.

You can achieve this by manually creating a for loop that iterates over the original map and adds each key-value pair to the new map:

for k, v := range originalMap {
    newMap[k] = v
}

By doing this, you create a new map that contains the same data as the original map, but the two maps are separate objects with their own copies of the data.

Clearing the Original Map

Once you have a deep copy of the map, you can clear the original map without affecting the new map. To do this, you can use the reset function from the container/list package:

for something := range fruits {
    aMap := make(map[string]aStruct)
    aSuperMap := make(map[string]map[string]aStruct)

    for x := range something {
        aMap[x] = aData
        aSuperMap[y] = aMap
        delete(aMap, x)
    }

    // Deep copy aMap into newMap
    newMap := make(map[string]aStruct)
    for k, v := range aMap {
        newMap[k] = v
    }

    // Clear the original aMap
    container/list.Reset(aMap)

    // Save the new map
    saveASuperMap(newMap)
}

By following these steps, you can successfully create an associative map in Go, perform a deep copy of the map to preserve the original data, and clear the original map to accept new values in the next loop iteration.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3