Go judges whether there is a key in the map
The method of golang to judge whether the key exists in the map is: [value,ok:map[key]], when ok is true, it exists, such as [_,ok:demo[“a”]fmt.Println(ok)].
The judgment method is value, ok := map[key], if ok is true, it exists.
Recommended video tutorial: go basic tutorial
Examples are as follows:
package main
import "fmt"
func main() {
demo := map[string]bool{
"a": false,
}
//Error, a exists, but returns false
fmt.Println(demo["a"])
//Correct judgment method
_, ok := demo["a"]
fmt.Println(ok)
}
false
true