golang map addition, deletion and modification

王林
Release: 2023-05-15 10:26:07
Original
636 people have browsed it

Golang is a very popular programming language and is widely used in web development, system programming, network programming and other fields. In golang, map is a very commonly used data structure, which can easily add, delete, and modify key-value pairs. This article will introduce the basic usage of map data structure and how to add, delete and modify map.

1. Basic usage of map data structure

map is a key-value pair data structure in golang. It can store different types of keys and values. Its usage is similar to that of a dictionary. It can be used by key to access the corresponding value. In golang, we can use the make function to create a map object. The syntax is as follows:

m := make(map[keyType] valueType)
Copy after login

Among them, keyType represents the type of key, and valueType represents the type of value. After creating the map object, we can get the corresponding value through the key, and we can use the len function to get the number of elements in the map. Map also provides a series of built-in functions, such as the delete function to delete specified key-value pairs, and the range function to traverse all elements in the map.

The following is a simple sample code that demonstrates the basic usage of map:

package main

import "fmt"

func main() {

// 创建map对象 var m map[string]int fmt.Println(m) // 输出空map对象 // 使用make函数创建map对象 m = make(map[string]int) fmt.Println(m) // 输出空map对象 // 添加键值对 m["one"] = 1 m["two"] = 2 m["three"] = 3 fmt.Println(m) // 输出{"one": 1, "two": 2, "three": 3} fmt.Println(len(m)) // 输出3 fmt.Println(m["three"]) // 输出3 // 删除键值对 delete(m, "two") fmt.Println(m) // 输出{"one": 1, "three": 3} fmt.Println(len(m)) // 输出2 // 遍历map for k, v := range m { fmt.Println(k, v) }
Copy after login

}

2. Map addition, deletion and modification operations

  1. Add key-value pairs to map

In golang, add key-value pairs to map Adding a new key-value pair is very simple, just use the subscript operator []. If the key does not exist, a new key-value pair is created; if the key already exists, the original value is overwritten. Here is a sample code:

m := make(map[string]int)
m["one"] = 1
m["two"] = 2
m[ "three"] = 3

This code creates an empty map object and then adds three key-value pairs to it. If you want to add multiple key-value pairs to the map, you can use a loop statement to achieve this.

  1. map deletes key-value pairs

To delete key-value pairs in the map, you can use the built-in function delete provided by golang. The delete function needs to pass in two parameters. The first parameter represents the map object to be deleted, and the second parameter represents the key to be deleted. If the key exists, its corresponding value is deleted; if the key does not exist, the function does nothing.

The following is a sample code:

m := make(map[string]int)
m["one"] = 1
m["two"] = 2
m["three"] = 3

delete(m, "two") // Delete the key-value pair with key "two"

  1. map modification key Value pairs

It is also very simple to modify the key-value pairs in the map. You only need to use the subscript operator [] to achieve it. If the key does not exist, a new key-value pair is created; if the key already exists, the original value is overwritten. Here is a sample code:

m := make(map[string]int)
m["one"] = 1

m["one"] = 10 // Modify the value of key to "one" to 10

  1. Safely obtain the value in the map

When we obtain the value in the map, if the corresponding key does not exists, map will return a zero value of that value type, which may cause some problems. To avoid this, we can use ok-idiom to check. ok-idiom means that when obtaining the value in the map, first use a bool type variable to receive whether the key exists, and then use the bool value to determine whether the value can be obtained safely. Here is a sample code:

m := make(map[string]int)
m["one"] = 1

if val, ok := m["two "]; ok {

fmt.Println(val)
Copy after login

} else {

fmt.Println("key not exist")
Copy after login

}

In the above code, we first try to get the value with key "two". If the key If it does not exist, "key not exist" will be output. Using ok-idiom can avoid potential bugs caused by encountering non-existent keys when using map.

Summary

This article introduces the basic usage of map data structure in golang, and how to add, delete and modify map. Mastering the basic usage of map is very important for golang programmers, and it will be used frequently in actual development. When using map, we need to pay attention to avoid problems such as null pointer exceptions to ensure that the program runs normally.

The above is the detailed content of golang map addition, deletion and modification. 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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!