【Golang】map 基础

Posted by 西维蜀黍 on 2020-03-15, Last Modified on 2021-09-21

定义 map

可以使用内建函数 make 也可以使用 map 关键字来定义 Map:

/* 声明变量,默认 map 是 nil */
var map_variable map[key_data_type]value_data_type

/* 使用 make 函数 */
map_variable := make(map[key_data_type]value_data_type)

如果不初始化 map,那么 map_variable 就是一个 nil。

nil map 不能写入键值对,但是可以读取内部的值(虽然这并没有任何意义,因为一定会得到已知的结果,即 value 为 0)。

package main

func main() {
	var a map[int]int
	println(a[22]) // Return 0

	a[1] = 33 // https://blog.golang.org/maps
}

map的创建

The make function allocates and initializes a hash map data structure and returns a map value that points to it.

make(map[KeyType]ValueType, initialCapacity)

make(map[KeyType]ValueType)

map[KeyType]ValueType{}

map[KeyType]ValueType{ key1 : value1, key2: value2, ... , keyN : valueN}

如下,用4种方式分别创建数组,其中第一种和第二种的区别在于,有没有指定初始容量。

A new, empty map value is made using the built-in function make, which takes the map type and an optional capacity hint as arguments:

make(map[string]int)
make(map[string]int, 100)

The initial capacity does not bound its size: maps grow to accommodate the number of items stored in them, with the exception of nil maps. A nil map is equivalent to an empty map except that no elements may be added.

虽然map拥有一个基本特性,即一旦容量不够,它会自动扩容。但是预先为 map 指定 initial capacity 可以提高 performance。

func test1() {
    map1 := make(map[string]string, 5)
    map2 := make(map[string]string)
    map3 := map[string]string{}
    map4 := map[string]string{"a": "1", "b": "2", "c": "3"}
    fmt.Println(map1, map2, map3, map4)
}

Map 的元素数量

The number of map elements is called its length. For a map m, it can be discovered using the built-in function len and may change during execution.

map的查找

This statement retrieves the value stored under the key "route" and assigns it to a new variable i:

i := m["route"]

If the requested key doesn’t exist, we get the value type’s zero value. In this case the value type is int, so the zero value is 0:

j := m["root"]
// j == 0

A two-value assignment tests for the existence of a key:

i, ok := m["route"]

In this statement, the first value (i) is assigned the value stored under the key "route". If that key doesn’t exist, i is the value type’s zero value (0). The second value (ok) is a bool that is true if the key exists in the map, and false if not.

To test for a key without retrieving the value, use an underscore in place of the first value:

_, ok := m["route"]
package main

import "fmt"

func main() {
	map4 := map[string]string{"a": "1", "b": "2", "c": "3"}
	val, exist := map4["a"]
	val2, exist2 := map4["d"]
	fmt.Printf("%v,%v\n", exist, val)
	fmt.Printf("%v,%v\n", exist2, val2)
}

//output
true,1
false,

map的添加(修改)和遍历

实例

package main

import "fmt"

func main() {
	var countryCapitalMap map[string]string     //声明集合
	countryCapitalMap = make(map[string]string) //实例化集合

	// map插入key - value对,各个国家对应的首都 
	countryCapitalMap [ "France" ] = "巴黎"
	countryCapitalMap [ "Italy" ] = "罗马"
	countryCapitalMap [ "Japan" ] = "东京"
	countryCapitalMap [ "India " ] = "新德里"

	//使用键输出地图值 
	for country := range countryCapitalMap {
		fmt.Println(country, "首都是", countryCapitalMap [country])
	}

	//查看元素在集合中是否存在 
	capital, ok := countryCapitalMap [ "American" ] //如果确定是真实的,则存在,否则不存在
	//fmt.Println(capital)
	//fmt.Println(ok)
	if ok {
		fmt.Println("American 的首都是", capital)
	} else {
		fmt.Println("American 的首都不存在")
	}
}


//output
France 首都是 巴黎
Italy 首都是 罗马
Japan 首都是 东京
India  首都是 新德里
American 的首都不存在
var countryCapitalMap map[string]string     //声明集合
countryCapitalMap = make(map[string]string) //实例化集合

// map插入key - value对,各个国家对应的首都
countryCapitalMap [ "France" ] = "巴黎"
countryCapitalMap [ "Italy" ] = "罗马"
countryCapitalMap [ "Japan" ] = "东京"
countryCapitalMap [ "India " ] = "新德里"

//使用键输出地图值
for key, value := range countryCapitalMap {
	println(key)
	fmt.Println(value)
}

//ouput:
France
巴黎
Italy
罗马
Japan
东京
India 
新德里

map的删除 - delete() 函数

delete() 函数用于删除集合的元素, 参数为 map 和其对应的 key。实例如下:

实例

package main

import "fmt"

func main() {
   // 创建map 
   countryCapitalMap := map[string]string{"France": "Paris", "Italy": "Rome", "Japan": "Tokyo", "India": "New delhi"}

    fmt.Println("原始地图")

    // 打印地图
    for country := range countryCapitalMap {
        fmt.Println(country, "首都是", countryCapitalMap [ country ])
    }

    // 删除元素
    delete(countryCapitalMap, "France")
    fmt.Println("法国条目被删除")

    fmt.Println("删除元素后地图")
    // 打印地图
    for country := range countryCapitalMap {
        fmt.Println(country, "首都是", countryCapitalMap [ country ])
    }
}

以上实例运行结果为:

原始地图
India 首都是 New delhi
France 首都是 Paris
Italy 首都是 Rome
Japan 首都是 Tokyo
法国条目被删除
删除元素后地图
Italy 首都是 Rome
Japan 首都是 Tokyo
India 首都是 New delhi

函数传递 map

// makemap implements Go map creation for make(map[k]v, hint).
func makemap(t *maptype, hint int, h *hmap) *hmap {

As you see, the type of the value returned from runtime.makemap is a pointer to a runtime.hmap structure.

Maps, like channels, but unlike slices, are just pointers to runtime types. As you saw above, a map is just a pointer to a runtime.hmap structure.

因此,当通过函数传递 map时,在函数内部对 map 的修改,会体现到函数之外。

多维map

For example, this map of maps could be used to tally web page hits by country:

hits := make(map[string]map[string]int)

This is map of string to (map of string to int). Each key of the outer map is the path to a web page with its own inner map. Each inner map key is a two-letter country code. This expression retrieves the number of times an Australian has loaded the documentation page:

n := hits["/doc/"]["au"]

Unfortunately, this approach becomes unwieldy when adding data, as for any given outer key you must check if the inner map exists, and create it if needed:

func add(m map[string]map[string]int, path, country string) {
    mm, ok := m[path]
    if !ok {
        mm = make(map[string]int)
        m[path] = mm
    }
    mm[country]++
}
add(hits, "/doc/", "au")

On the other hand, a design that uses a single map with a struct key does away with all that complexity:

type Key struct {
    Path, Country string
}
hits := make(map[Key]int)

When an Vietnamese person visits the home page, incrementing (and possibly creating) the appropriate counter is a one-liner:

hits[Key{"/", "vn"}]++

And it’s similarly straightforward to see how many Swiss people have read the spec:

n := hits[Key{"/ref/spec", "ch"}]

Reference