我知道我可以遍历映射m
for k, v := range m { ... }
寻找一个键,但是有没有更有效的方法来测试一个键在地图中的存在呢?
我知道我可以遍历映射m
for k, v := range m { ... }
寻找一个键,但是有没有更有效的方法来测试一个键在地图中的存在呢?
当前回答
var empty struct{}
var ok bool
var m map[string]struct{}
m = make(map[string]struct{})
m["somestring"] = empty
_, ok = m["somestring"]
fmt.Println("somestring exists?", ok)
_, ok = m["not"]
fmt.Println("not exists?", ok)
然后,去查地图,去 somestring存在吗?真正的 不存在?假
其他回答
var empty struct{}
var ok bool
var m map[string]struct{}
m = make(map[string]struct{})
m["somestring"] = empty
_, ok = m["somestring"]
fmt.Println("somestring exists?", ok)
_, ok = m["not"]
fmt.Println("not exists?", ok)
然后,去查地图,去 somestring存在吗?真正的 不存在?假
除了《Go编程语言规范》,你还应该阅读《Effective Go》。在地图的部分,他们说,除了别的以外:
An attempt to fetch a map value with a key that is not present in the map will return the zero value for the type of the entries in the map. For instance, if the map contains integers, looking up a non-existent key will return 0. A set can be implemented as a map with value type bool. Set the map entry to true to put the value in the set, and then test it by simple indexing. attended := map[string]bool{ "Ann": true, "Joe": true, ... } if attended[person] { // will be false if person is not in the map fmt.Println(person, "was at the meeting") } Sometimes you need to distinguish a missing entry from a zero value. Is there an entry for "UTC" or is that 0 because it's not in the map at all? You can discriminate with a form of multiple assignment. var seconds int var ok bool seconds, ok = timeZone[tz] For obvious reasons this is called the “comma ok” idiom. In this example, if tz is present, seconds will be set appropriately and ok will be true; if not, seconds will be set to zero and ok will be false. Here's a function that puts it together with a nice error report: func offset(tz string) int { if seconds, ok := timeZone[tz]; ok { return seconds } log.Println("unknown time zone:", tz) return 0 } To test for presence in the map without worrying about the actual value, you can use the blank identifier (_) in place of the usual variable for the value. _, present := timeZone[tz]
在“索引表达式”中提到。
赋值中使用的map[K]V类型映射a上的索引表达式 或者初始化特殊形式 V, ok = a[x] V, ok:= a[x] Var v, ok = a[x] 产生一个额外的无类型布尔值。ok的值为true,如果 键x存在于映射中,否则为false。
下面是检查映射是否包含键的方法。
val, ok := myMap["foo"]
// If the key exists
if ok {
// Do something
}
这将初始化两个变量。Val是映射中“foo”的值(如果存在),如果不存在则为“0值”(在本例中为空字符串)。Ok是bool类型,如果键存在,它将被设置为true。
如果愿意,可以将其缩短为一行代码。
if val, ok := myMap["foo"]; ok {
//do something here
}
Go允许您在if语句中的条件(注意分号)之前放置初始化语句。这样做的结果是,ofval和ok的作用域将被限制在if语句的主体中,如果你只需要在那里访问它们,这是很有帮助的。
var d map[string]string
value, ok := d["key"]
if ok {
fmt.Println("Key Present ", value)
} else {
fmt.Println(" Key Not Present ")
}