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密钥