How to use mapping types in Go language?
In the Go language, map is a built-in data structure used to store key-value pairs. Maps allow quick retrieval of values by key, similar to dictionaries or hash tables in other languages. In this article, we will introduce the basic concepts of map types in Go language and how to use maps to store and retrieve data.
To create a map, you can use the make
function in the Go language to initialize an empty map and specify the key and value types. Here is a simple example:
package main import "fmt" func main() { // 创建一个映射,键是字符串类型,值是整数类型 m := make(map[string]int) // 添加键值对到映射中 m["apple"] = 10 m["banana"] = 5 fmt.Println(m) // 输出map[apple:10 banana:5] }
In the above example, we have created a map m
where the key type is string and the value type is integer. Then, we added two sets of key-value pairs to the map, namely apple:10
and banana:5
. Finally, use the fmt.Println
function to output the entire map.
To access elements in a map, the map can be indexed by key. If the key exists, the corresponding value will be returned; if the key does not exist, a zero value of the value type will be returned. The example is as follows:
package main import "fmt" func main() { m := make(map[string]int) m["apple"] = 10 m["banana"] = 5 // 访问映射中的元素 fmt.Println("apple:", m["apple"]) // 输出:apple: 10 fmt.Println("orange:", m["orange"]) // 输出:orange: 0 }
In the above example, we access the values of the apple
and orange
keys in the map. If the key exists, the corresponding value is printed. ;If the key does not exist, print the zero value of the value type.
If you need to modify an element in the map, just reassign it through the key. An example is as follows:
package main import "fmt" func main() { m := make(map[string]int) m["apple"] = 10 fmt.Println("apple:", m["apple"]) // 输出:apple: 10 m["apple"] = 20 // 修改键值对 fmt.Println("apple:", m["apple"]) // 输出:apple: 20 }
In the above example, we modify the value of the apple
key in the map from 10 to 20 and output the modified result.
You can use the delete
function to delete elements in the map. An example is as follows:
package main import "fmt" func main() { m := make(map[string]int) m["apple"] = 10 fmt.Println("apple:", m["apple"]) // 输出:apple: 10 delete(m, "apple") // 删除键值对 fmt.Println("apple:", m["apple"]) // 输出:apple: 0 }
In the above example, we use the delete
function to delete the apple
key-value pair in the map, and then access apple
Key will return zero value of value type.
You can use the for range
statement to iterate the key-value pairs in the map. The example is as follows:
package main import "fmt" func main() { m := make(map[string]int) m["apple"] = 10 m["banana"] = 5 // 遍历映射 for k, v := range m { fmt.Println(k, ":", v) } }
In the above example, we use the for range
statement to traverse all key-value pairs in the mapping m
, and output the keys and values in sequence .
When using mapping, you need to pay attention to the following points:
Summary: This article introduces how to create, access, modify, delete and iterate mapping types in Go language. Through the introduction of the basic operations and precautions of mapping, readers can better understand and use the mapping data structure in the Go language. Hope this article is helpful to you.
The above is the detailed content of How to use mapping types in Go language?. For more information, please follow other related articles on the PHP Chinese website!