Home > Backend Development > Golang > golang query dictionary length

golang query dictionary length

PHPz
Release: 2023-05-10 15:11:37
Original
736 people have browsed it

Go language (golang) is a programming language that is as simple, efficient and safe as possible. In the development of Golang, it is often necessary to query and operate dictionaries. This article will introduce how to query the dictionary length in golang.

In Golang, a dictionary is an unordered data structure, also called a map. It consists of a series of unordered key-value pairs, each key uniquely corresponds to a value. Therefore, we can quickly query and operate the dictionary by leveraging the uniqueness of key-value pairs.

How to query the dictionary length:

In Golang, the method of querying the dictionary length is very simple. Just use the built-in len() function. The len() function returns the number of key-value pairs in the Map. An example is as follows:

package main

import "fmt"

func main() {
    student := map[string]int{
        "Tom":   18,
        "Alex":  20,
        "Peter": 22,
    }
    fmt.Println(len(student))
}
Copy after login

The running result is:

3
Copy after login

In the above code, we define a student variable, which is a dictionary composed of string type keys and integer type values. . Use the len() function to query the length of the dictionary to be 3.

However, it should be noted that when querying the length of a Map, we can only know how many key-value pairs it has, but cannot know the specific keys and values.

In the actual development process, we may need to query the dictionary based on a specific key value. Golang provides a method to query the value of a key in the dictionary. The example is as follows:

package main

import "fmt"

func main() {
    student := map[string]int{
        "Tom":   18,
        "Alex":  20,
        "Peter": 22,
    }
    age, ok := student["Tom"]
    if ok {
        fmt.Println("Tom's age is ", age)
    } else {
        fmt.Println("Tom is not in the student list")
    }
}
Copy after login

The running result is:

Tom's age is  18
Copy after login

In the above code, we first define a student variable, which is a A dictionary consisting of keys of type string and values ​​of type integer. Next, we use student["Tom"] to query the value corresponding to the key Tom. Since Tom exists in the dictionary, the value of the age variable is 18. At the same time, we also used the ok variable to determine whether Tom exists in the dictionary. Since Tom does exist in the dictionary, the value of ok is true.

Summary:

In Golang, querying the dictionary length is very simple, just call the built-in len() function. At the same time, we can also query specific values ​​in the dictionary based on key values. These methods can help us operate and query the dictionary more efficiently, thereby better completing our development work.

The above is the detailed content of golang query dictionary length. 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