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

5年以前  |  阅读数:528 次  |  编程语言:Golang 

判断方式为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)
}

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8