Home  >  Article  >  Backend Development  >  Golang determines whether key exists in map

Golang determines whether key exists in map

王林
王林Original
2020-02-17 10:43:253401browse

Golang determines whether key exists in map

The judgment method is value, ok := map[key], if ok is true, it exists.

Recommended video tutorial: go basic tutorial

The example is as follows:

package main
 
import "fmt"
 
func main() {
 
    demo := map[string]bool{
        "a": false,
    }
 
    //错误,a存在,但是返回false
    fmt.Println(demo["a"])
 
    //正确判断方法
    _, ok := demo["a"]
    fmt.Println(ok)
}

Output:

false
true

More related content to learn, Please visit the Programming Tutorial column of the php Chinese website!

The above is the detailed content of Golang determines whether key exists in map. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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