Go language mapping between container provided to map
its internal 散列表(hash)
implementation.
map
map is based on a disordered key-value
data structure, Go language map is a reference type, it must be initialized before use.
map definitions
Go language map
syntax defined as follows:
map[KeyType]ValueType
among them,
- KeyType: indicates the type of bond.
- ValueType: represents the type of the value corresponding to the key.
map type variable default initial value is nil, you need to use the make () function to allocate memory. The syntax is:
make(map[KeyType]ValueType, [cap])
Where the cap represents the map capacity, although this parameter is not required, but we should assign it an appropriate capacity when you initialize the map.
Basic use map
The map data is occurring in pairs, the basic map sample code is as follows:
func main() { scoreMap := make(map[string]int, 8) scoreMap["张三"] = 90 scoreMap["小明"] = 100 fmt.Println(scoreMap) fmt.Println(scoreMap["小明"]) fmt.Printf("type of a:%T\n", scoreMap) }
Output:
map[小明:100 张三:90]
100
type of a:map[string]int
map also supports filling element when it was declared, for example:
func main() { userInfo := map[string]string{ "username": "沙河小王子", "password": "123456", } fmt.Println(userInfo) // }
Determine whether there is a key
Go written language has a special key exists in the map is determined in the following format:
value, ok := map[key]
for example:
func main() { scoreMap := make(map[string]int) scoreMap["张三"] = 90 scoreMap["小明"] = 100 // 如果key存在ok为true,v为对应的值;不存在ok为false,v为值类型的零值 v, ok := scoreMap["张三"] if ok { fmt.Println(v) } else { fmt.Println("查无此人") } }
map traversal
Go language used for range
to traverse map.
func main() { scoreMap := make(map[string]int) scoreMap["张三"] = 90 scoreMap["小明"] = 100 scoreMap["娜扎"] = 60 for k, v := range scoreMap { fmt.Println(k, v) } }
But we just want to traverse the key, you can press the following wording:
func main() { scoreMap := make(map[string]int) scoreMap["张三"] = 90 scoreMap["小明"] = 100 scoreMap["娜扎"] = 60 for k := range scoreMap { fmt.Println(k) } }
Note: The order of the elements when traversing the map regardless of the order to add key-value pairs.
Use delete () function to remove key-value pairs
Use delete()
the built-in function to remove the key from the map a set of pairs delete()
formatting functions as follows:
delete(map, key)
among them,
- map: delete key-value pairs map
- key: the key to be deleted represents key-value pairs
Sample code is as follows:
func main(){ scoreMap := make(map[string]int) scoreMap["张三"] = 90 scoreMap["小明"] = 100 scoreMap["娜扎"] = 60 delete(scoreMap, "小明")//将小明:100从map中删除 for k,v := range scoreMap{ fmt.Println(k, v) } }
Traversal order specified map
func main() { rand.Seed(time.Now().UnixNano()) //初始化随机数种子 var scoreMap = make(map[string]int, 200) for i := 0; i < 100; i++ { key := fmt.Sprintf("stu%02d", i) //生成stu开头的字符串 value := rand.Intn(100) //生成0~99的随机整数 scoreMap[key] = value } //取出map中的所有key存入切片keys var keys = make([]string, 0, 200) for key := range scoreMap { keys = append(keys, key) } //对切片进行排序 sort.Strings(keys) //按照排序后的key遍历map for _, key := range keys { fmt.Println(key, scoreMap[key]) } }
Type of map elements sliced
The following code shows the operation of element sections when a map type:
func main() { var mapSlice = make([]map[string]string, 3) for index, value := range mapSlice { fmt.Printf("index:%d value:%v\n", index, value) } fmt.Println("after init") // 对切片中的map元素进行初始化 mapSlice[0] = make(map[string]string, 10) mapSlice[0]["name"] = "小王子" mapSlice[0]["password"] = "123456" mapSlice[0]["address"] = "沙河" for index, value := range mapSlice { fmt.Printf("index:%d value:%v\n", index, value) } }
Slice type values map
The following code shows the map value slice types of actions:
func main() { var sliceMap = make(map[string][]string, 3) fmt.Println(sliceMap) fmt.Println("after init") key := "中国" value, ok := sliceMap[key] if !ok { value = make([]string, 0, 2) } value = append(value, "北京", "上海") sliceMap[key] = value fmt.Println(sliceMap) }