Home  >  Article  >  Backend Development  >  How to delete elements in golang map

How to delete elements in golang map

青灯夜游
青灯夜游Original
2022-12-08 18:26:148506browse

Two methods to delete map elements: 1. Use the delete() function to delete the specified key-value pair from the map, the syntax is "delete(map, key name)"; 2. Re-create a new map object , you can clear all elements in the map, the syntax is "var mapname map[keytype]valuetype".

How to delete elements in golang map

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Map in Go language is a special data structure, an unordered collection of element pairs (pair). Pair corresponds to a key (index) and a value (value), so this structure is also called Associative array or dictionary is an ideal structure that can quickly find values. Given a key, the corresponding value can be quickly found.

Deleting and clearing map elements in Go language

1. Use the delete() function to delete key-value pairs from the map

The Go language provides a built-in function delete() for deleting elements in a container. Let's briefly introduce how to use the delete() function to delete elements in a map.

Use the delete() built-in function to delete a set of key-value pairs from the map. The format of the delete() function is as follows:

delete(map, 键名)

where map is the map instance to be deleted, and the key is the The key of the key-value pair in the deleted map. [Related recommendations: Go Video Tutorial, Programming Teaching]

Deleting a set of key-value pairs from the map can be accomplished by the following code:

scene := make(map[string]int)

// 准备map数据
scene["route"] = 66
scene["brazil"] = 4
scene["china"] = 960

delete(scene, "brazil")

for k, v := range scene {
    fmt.Println(k, v)
}

The code output is as follows:

How to delete elements in golang map

In this example, the delete() function is used to delete brazil from the scene map.

2. Clear all elements in the map

What’s interesting is that the Go language does not provide any functions or methods for clearing all elements in the map. Clearing the map The only way is to re-create a new map without worrying about the efficiency of garbage collection. Parallel garbage collection in the Go language is much more efficient than writing a clearing function.

Method to declare a new map:

var mapname map[keytype]valuetype

Where:

  • mapname is the variable name of the map.

  • keytype is the key type.

  • valuetype is the value type corresponding to the key.

Tip: Spaces are allowed between [keytype] and valuetype.

You do not need to know the length of the map when declaring it, because the map can grow dynamically. The value of an uninitialized map is nil. You can use the function len() to get the number of pairs in the map.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of How to delete elements in golang 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