备忘录吕吕没有备忘录十新建
Create map
建立地图
Add item to map
将商品添加到地图
Lookup item from map
从地图查找项目
Delete item from map
从地图上删除项目
Delete all content from a map
从地图上删除所有内容
Count the items in a map
计算地图中的项目
Put the keys of a map into a slice
将地图的关键点切成薄片
建立地图 (Create map)
// define mapvar m1 map[int]string// instantiate mapm1 = make(map[int]string)// implict map instantiationm2 := make(map[int]string)// map literalm3 := map[int]string{1: "One",9: "Nine"}
将商品添加到地图 (Add item to map)
m := map[int]string{1: "One",9: "Nine"}m[10] = "Ten"// m contains 1, 9, 10
从地图查找项目 (Lookup item from map)
m := map[int]string{1: "One",9: "Nine"}item, ok := m[9] //item is "Nine; ok is true if the//key was found, false otherwise
从地图上删除项目 (Delete item from map)
m := map[int]string{1: "One",9: "Nine"}delete(m, 9)//m contains only the `1` item
从地图上删除所有内容 (Delete all content from a map)
Assign to the variable pointing to the map a new map:
为指向地图的变量分配一个新地图:
m := map[int]string{1: "One",9: "Nine"}m = make(map[int]string) // m is an initialized, empty map
计算地图中的项目 (Count the items in a map)
m := map[int]string{1: "One",9: "Nine"}l := len(m) // l == 2
将地图的关键点切成薄片 (Put the keys of a map into a slice)
keys := make([]string, 0, len(m))for k := range m {keys = append(keys, k)}
翻译自: /golang-cheat-sheet-maps/
备忘录吕吕没有备忘录十新建