Home > Backend Development > Golang > How to use mapping types in Go language?

How to use mapping types in Go language?

PHPz
Release: 2024-03-23 10:12:04
Original
657 people have browsed it

How to use mapping types in Go language?

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.

Create a map

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]
}
Copy after login

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.

Accessing and modifying elements in a 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
}
Copy after login

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
}
Copy after login

In the above example, we modify the value of the apple key in the map from 10 to 20 and output the modified result.

Delete elements in the map

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
}
Copy after login

In the above example, we use the delete function to delete the apple key-value pair in the map, and then access appleKey will return zero value of value type.

Iterate the map

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)
    }
}
Copy after login

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 .

Notes

When using mapping, you need to pay attention to the following points:

  1. Mapping is a reference type and stores references to key-value pairs, so in Passing a map when a function is called does not produce a copy.
  2. The keys in the map must be comparable types, such as basic types, pointer types, array types, structure types, etc.
  3. The values ​​in the mapping can be of any type, including functions, slices, maps, etc.

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template