Home  >  Article  >  Backend Development  >  golang clear map

golang clear map

WBOY
WBOYOriginal
2023-05-19 11:06:401398browse

In Golang, map is a very important data type, which allows us to store and access data in the form of key-value pairs. However, when we use map, we will inevitably encounter situations where we need to clear the map. This article will introduce three methods of clearing map in Golang.

  1. Reallocate a new map directly

This is the simplest and most direct method. We can directly reassign a new map to overwrite the original map, thus clearing all key-value pairs. The sample code is as follows:

package main

import "fmt"

func main() {
    m := map[string]int{"a": 1, "b": 2}
    fmt.Println("Before clearing:", m)

    m = make(map[string]int)
    fmt.Println("After clearing:", m)
}

The output result is as follows:

Before clearing: map[a:1 b:2]
After clearing: map[]

This method is more suitable for less data in the map, because for a map with a large amount of data, a new map must be reallocated Can result in very expensive memory allocation and garbage collection.

  1. Use a for loop to delete key-value pairs

Another way to clear the map is to use a for loop to delete all key-value pairs. Compared with directly reallocating a new map, this method is more suitable for maps with large amounts of data. The sample code is as follows:

package main

import "fmt"

func main() {
    m := map[string]int{"a": 1, "b": 2}
    fmt.Println("Before clearing:", m)

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

    fmt.Println("After clearing:", m)
}

The output is as follows:

Before clearing: map[a:1 b:2]
After clearing: map[]

This method uses a for loop to traverse all the keys in the map and delete them using the delete function. But it should be noted that during the process of traversing the map, we must use the range keyword to avoid modifying the map being traversed. Failure to do so will result in some key-value pairs being deleted while others being retained.

  1. Set the map length to 0

The last method is to set the map length to 0, so that all key-value pairs can be cleared. The sample code is as follows:

package main

import "fmt"

func main() {
    m := map[string]int{"a": 1, "b": 2}
    fmt.Println("Before clearing:", m)

    for i := range m {
        delete(m, i)
    }
    m = make(map[string]int, 0)

    fmt.Println("After clearing:", m)
}

The output result is as follows:

Before clearing: map[a:1 b:2]
After clearing: map[]

This method can avoid reallocating memory and avoiding the use of for loops. But it should be noted that before setting the length of the map to 0, we still need to use a for loop to delete all key-value pairs in the map. Otherwise, the map will still retain some key-value pairs, but their values ​​will be zero.

To sum up, we can use three different methods to clear the map. The best approach depends on the size of the map and the required clearing performance. But no matter which method you use, you should take care to protect the data in the map to avoid accidentally deleting important key-value pairs.

The above is the detailed content of golang clear map. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:How to log in golangNext article:How to log in golang