Home  >  Article  >  Backend Development  >  Golang's method to determine if the key in the map does not exist

Golang's method to determine if the key in the map does not exist

尚
Original
2019-12-24 11:11:079288browse

Golang's method to determine if the key in the map does not exist

#go languageIn map, map is a set of kv pairs. The bottom layer uses a hash table and a linked list to resolve conflicts. Through the compiler and runtime, all map objects share the same code.

Golang’s method of judging whether the key in the map does not exist:

The judgment method is value,ok := map[key], if ok is true, it exists, if ok is false, the key of the map does not exist. .

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

For more golang knowledge, please pay attention to the golang tutorial column on the PHP Chinese website.

The above is the detailed content of Golang's method to determine if the key in the map does not exist. 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