Go语言的map如何判断key是否存在

判断方式为value,ok := map[key], ok为true则存在

package main

import "fmt"

func main() {

    demo := map[string]bool{
        "a": false,
    }

    //错误,a存在,但是返回false
    fmt.Println(demo["a"])

    //正确判断方法
    _, ok := demo["a"]
    fmt.Println(ok)
}

输出

false
true

  

 

posted @ 2018-01-12 21:40  雪山飞猪  阅读(51421)  评论(3)    收藏  举报