How to clear a map in Go?

I'm looking for something like the c++ function .clear() for the primitive type map.

Or should I just create a new map instead?

Update: Thank you for your answers. By looking at the answers I just realized that sometimes creating a new map may lead to some inconsistency that we don't want. Consider the following example:

var a map[string]string
var b map[string]string

func main() {
    a = make(map[string]string)
    b=a
    a["hello"]="world"
    a = nil
    fmt.Println(b["hello"])
}

I mean, this is still different from the .clear() function in c++, which will clear the content in the object.

Answers


You should probably just create a new map. There's no real reason to bother trying to clear an existing one, unless the same map is being referred to by multiple pieces of code and one piece explicitly needs to clear out the values such that this change is visible to the other pieces of code.

So yeah, you should probably just say

mymap = make(map[keytype]valtype)

If you do really need to clear the existing map for whatever reason, this is simple enough:

for k := range m {
    delete(m, k)
}

Unlike C++, Go is a garbage collected language. You need to think things a bit differently.

When you make a new map

a := map[string]string{"hello": "world"}
a = make(map[string]string)

the original map will be garbage-collected eventually; you don't need to clear it manually. But remember that maps (and slices) are reference types; you create them with make(). The underlying map will be garbage-collected only when there are no references to it. Thus, when you do

a := map[string]string{"hello": "world"}
b := a
a = make(map[string]string)

the original array will not be garbage collected (until b is garbage-collected or b refers to something else).


If you are trying to do this in a loop, you can take advantage of the initialization to clear out the map for you. For example:

for i:=0; i<2; i++ {
    animalNames := make(map[string]string)
    switch i {
        case 0:
            animalNames["cat"] = "Patches"
        case 1:
            animalNames["dog"] = "Spot";
    }

    fmt.Println("For map instance", i)
    for key, value := range animalNames {
        fmt.Println(key, value)
    }
    fmt.Println("-----------\n")
}

When you execute this, it clears out the previous map and starts with an empty map. This is verified by the output:

$ go run maptests.go 
For map instance 0
cat Patches
-----------

For map instance 1
dog Spot
-----------

The value of an uninitialized map is nil.

map = nil; 

Should do. Then of course, you need to create a new map.


Need Your Help

Calculate median in c#

c# .net algorithm median

I need to write function that will accept array of decimals and it will find the median.

Eclipse with tomcat - eclipse modifies server.xml

java eclipse tomcat

I use tomcat with eclipse in 'Use tomcat installation' mode. My problem is, that eclipse overwrites tomcats server.xml every time, and deletes my crossContext="true" elements.