Home > Backend Development > Golang > How to Deep Copy a Go Map and Clear the Original Without Affecting the Copy?

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

Susan Sarandon
Release: 2024-12-30 13:05:09
Original
497 people have browsed it

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
}
Copy after login

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)
}
Copy after login

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.

The above is the detailed content of How to Deep Copy a Go Map and Clear the Original Without Affecting the Copy?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template