首页 > 代码库 > A Tour of Go Mutating Maps
A Tour of Go Mutating Maps
Insert or update an element in map m
:
m[key] = elem
Retrieve an element:
elem = m[key]
Delete an element:
delete(m, key)
Test that a key is present with a two-value assignment:
elem, ok = m[key]
If key
is in m
, ok
is true
. If not, ok
is false
and elem
is the zero value for the map‘s element type.
Similarly, when reading from a map if the key is not present the result is the zero value for the map‘s element type.
package main import "fmt"func main() { m := make(map[string]int) m["Answer"] = 42 fmt.Println("The value:", m["Answer"]) m["Answer"] = 48 fmt.Println("The value:", m["Answer"]) v, ok := m["Answer"] fmt.Println("The value:", v, "Present?", ok) delete(m, "Answer") fmt.Println("The value:", m["Answer"]) v2, ok2 := m["Answer"] fmt.Println("The value:", v2, "Present?", ok2)}
好变态的map用法,真不知道google是怎怎么想的
A Tour of Go Mutating Maps
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。