Skip to content
小飞侠
  • 首页
  • 小程序
  • uniapp
  • vue
  • APICloud
  • 其他
菜单

博客

  1. 首页>
  2. golang>
  3. golang map 判断key是否存在

golang map 判断key是否存在

  • Post author:xfxia
  • Post published:2023年8月23日
  • Post category:golang


判断方法示例代码

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

if _, ok := map[key]; !ok {
    // 不存在
}

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


Tags: golang, map

Read more articles

Previous Postgolang 堆内存栈内存管理
在下一篇文章Java实现List,Map,Set 遍历的多种方式

你可能也喜欢

golang反向代理使用

Golang编译报错 ‘invalid char’

Golang中defer、return、返回值和main、init函数的陷阱

golang 消息忽略 signal.Ignore(syscall.SIGHUP) 示例

Go语言学习笔记——Golang 1.18新特性工作区workspace

golang时间字符串和时间戳转换

golang生成UUID通用唯一识别码

Go语言学习笔记—golang接口与实现面向对象特性

golang设计模式——结构模式

golang:使用append为切片添加元素

golang判断字符串出现的位置及是否包含

Golang使用selenium、chromedp操作Chrome

Golang基础

Golang:context.Context

golang 上传图片到aws s3 bucket

golang之pprof

golang语言 map全方位介绍 【图文+代码】

深入理解 Golang: Go 程序的编译运行过程

golang入门系列之浅谈 goroutine

golang 内置函数

目录

  • APICloud (3)
  • golang (228)
  • java (8,236)
  • linux (4,002)
  • mysql (2,800)
  • php (1,070)
  • python (5,439)
  • solidity (25)
  • uniapp (235)
  • vue (2,463)
  • 其他 (88,233)
  • 小程序 (801)

文章归档

  • 2023年十二月 (1872)
  • 2023年十一月 (477)
  • 2023年十月 (17371)
  • 2023年九月 (44974)
  • 2023年八月 (26399)
  • 2023年七月 (14793)
  • 2023年六月 (1)
  • 2023年五月 (2186)
  • 2023年四月 (3838)
  • 2023年三月 (1544)
  • 2023年二月 (3)
  • 2023年一月 (4)
  • 2022年十二月 (3)
  • 2022年十一月 (36)
  • 2022年十月 (16)
  • 2022年九月 (8)
  • 2021年九月 (1)
  • 2020年九月 (6)
  • 2020年八月 (5)

标签

Aave (1) cosmos (6) defi (255) flag (27) gitee (1) github (1) golang (234) java (8234) linux命令 (82) List (1) map (1115) mysql (3093) npm (237) tendermint (6) uuid (43) vant (64) vmware (270) 区块链 (63) 合约 (37) 小程序版本更新 (3)
  • 首页
  • 小程序
  • uniapp
  • vue
  • APICloud
  • 其他
Copyright xfxia.com 鲁ICP备19024253号-2
关闭菜单