golang map determines whether key exists

(*-*)浩
Release: 2019-12-17 09:39:51
Original
2568 people have browsed it

golang map determines whether key exists

How does the map in Go language determine whether the key exists? Provides a method for judging whether an item is in the array. If this judgment is frequently used in the program, the array can be converted into a map with the members in the array as keys and then judged using the above method, which will improve the efficiency of judgment. The judgment method is value, ok := map[key], if ok is true, there is

if _, ok := map[key]; ok {
//存在
}
Copy after login

Output

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

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

Related labels:
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