It is very common to use map in golang, because map can quickly locate the data that needs to be accessed. However, when using map, we often encounter situations where we need to modify the values in the map. The way to modify map in golang is slightly different from other languages. Let us take a look at it below.
In golang, map is a reference type that can be created through the make function. The following is an example of creating a map:
m := make(map[string]int)
The key value types of the map are string and int respectively.
We can use the following method to add elements to the map:
m["one"] = 1
What if you want to modify the elements in the map? It should be noted that when modifying elements in a map in golang, you cannot use subscripts to modify them. Instead, you need to use the map's own methods of deleting and adding elements to achieve modifications. The following is an example of modifying the map:
m := make(map[string]int) m["one"] = 1 fmt.Println(m) // 修改元素 delete(m, "one") // 删除指定元素 m["two"] = 2 // 添加修改后的元素 fmt.Println(m)
As you can see, for the element "one" that needs to be modified, we need to delete the element through the delete function, and then add the modified element by adding elements. "two". The final output is the modified map{"two":2}.
However, it should be noted that if the element does not exist in the map, directly using the delete function to delete the element will cause the program to panic. Therefore, when modifying the map, you need to first determine whether the element exists in the map, and then perform the delete operation if it exists. The following is an example of determining whether an element exists and modifying the map:
m := make(map[string]int) m["one"] = 1 fmt.Println(m) // 判断元素是否存在 if _, ok := m["one"]; ok { // 存在则进行删除操作 delete(m, "one") m["two"] = 2 // 添加修改后的元素 } fmt.Println(m)
Through the above method, we can safely modify the elements in the map.
In addition to the above modification methods, there is also a more special modification method: using range to traverse the map to modify the elements in the map. Since range returns a copy of the elements in the map when traversing the map, rather than the element itself, you can modify the elements of the map by modifying the copy. The following is an example of using range traversal map to modify:
m := make(map[string]int) m["one"] = 1 fmt.Println(m) for key, value := range m { if key == "one" { m[key] = 2 // 修改副本 } } fmt.Println(m)
Through the above range traversal method, we achieve the purpose of modifying the elements in the map.
To sum up, the method of modifying map in golang is not as simple as in other languages, but by deleting and adding elements and using range traversal, we can achieve the purpose of modifying the elements in the map. Of course, in actual development, we need to choose the appropriate modification method according to specific needs.
The above is the detailed content of golang map modification. For more information, please follow other related articles on the PHP Chinese website!