golang rsa密钥_如何在Golang的地图中检查密钥是否存在?

golang rsa密钥

When you try to get the value of key in a map, you get two return values. The first value is the value of key and second is bool value which could be either true or false. If a key is present in the map then the second value will be true else false.

当您尝试获取映射中的key值时,您将获得两个返回值。 第一个值是key的值,第二个值是bool值,可以为true或false。 如果映射中存在键,则第二个值将为true,否则为false

If a key is not present in the map then the first value will be default zero value.

如果映射中不存在键,则第一个值为默认的零值。

Syntax:

句法:

    first_value, second_value := map_variable_name[key_name]
        or
    first_value := map_variable_name[key_name]
        or
    first_value, _ := map_variable_name[key_name]

Here, second_value is optional.

在这里, second_value是可选的。

Code example:

代码示例:

package main

import (
    "fmt"
)

func main() {
  m:= map[string]int{"apple": 1}

  value, ok := m["apple"]
  fmt.Println(value, ok)

  value, ok = m["mango"]
  fmt.Println(value, ok)
}

Output

输出量

1 true
0 false

Check key exists in map using if-statement

使用if语句在地图中检查密钥是否存在

package main

import (
    "fmt"
)

func main() {
  m:= map[string]int{"apple": 1}

  if value, ok := m["apple"]; ok {
    fmt.Printf("Apple is present in map. Value is: %d\n", value)
  } else {
    fmt.Printf("Apple is not present in map. Value is: %d\n", value)
  }

  if value, ok := m["mango"]; ok {
    fmt.Printf("Mango is present in map. Value is: %d\n", value)
  } else {
    fmt.Printf("Mango is not present in map. Value is: %d\n", value)
  }
}

Output

输出量

Apple is present in map. Value is: 1
Mango is not present in map. Value is: 0


翻译自: https://www.includehelp.com/golang/how-to-check-if-key-exists-in-a-map-in-golang.aspx

golang rsa密钥

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值