This is a creation in Article, where the information may have evolved or changed.
Map Study Notes
The map data structure in Golang is similar to HashMap in Java, capable of automatic expansion, non-concurrency security, and key is unordered.
about why in the Golang map is built-in data structure, you can see Golang official faq:https://golang.org/doc/faq#builtin_maps (domestic can use this site access: https:// Golang.google.cn/doc/faq#builtin_maps)
Basic usage of Map
How //Map is declared Var map1 map[keytype]valuetype//because the map is a reference type, the initialization must be displayed, otherwise the default value is Nilvar si map[string]intfmt. Println (si = nil)//truesi["Monday"] = 1//panic:assignment to entry in nil Map//map There are two main ways to initialize: Make mode and literal mode//1. Using Make allocates memory//with the default size var map1 map[string]int = Make (map[string]int)//display the specified size, if you want to put a large amount of data into the map, it is best to display the map size when creating a new one,// Doing so will cause the map to expand frequently, affecting the performance of var map1 map[string]int = Make (Map[string]int, +) map1["age"] = 1//2.literal mode initialization map1: = map[string] int{"Monday": 1, "Tuesday": Common operations for 2}map: Get, set, delete//1.set: Assign a value to map map1["Wednesday"] = 3//2.get: Get Valu from map according to specified key e//gets the value in the map based on key, if the specified key does not exist in the map//returns the ValueType "0 value" ("0 value" definition todo in Golang) fmt. Println (map1["Wednesday")//Print 3fmt. Println (map1["Hello"])//Print 0 (0 is a 0 value of type int)//To avoid this situation you can use the following assignment statement to test V, OK: = map1["Wednesday"]if V, OK: = map1["Wednes Day "]; OK {//process}//3.delete: Deletes the value from the map according to the specified key delete (Map1, "Monday")//4. The current size of the map is obtained through Len (), and the cap () FMT cannot be used on the map. Println (Len (map1))
Key description for
- map: The official documentation states that any type that defines the equal operation can be used as a key for the map, such as integers, floating point and complex numbers, strings, Pointers, Interfaces,channel, structs and arrays. Func, slice, and map cannot be keys because they do not define equal operations. For structs, interface, and arrays, if they are to be keys, they must contain elements that can be used as keys, as shown in the following test code:
Type person struct {name Stringage int}type Person2 struct {name stringage intfriends []string}//struct as key of map test var Test map[person]int = Make (map[person]int)//struct type as keytest[person{"Fuqiu", [] = 100fmt. Println (test[person{"Fuqiu", 20}])//output//interface as a key test for map Keyarray: = [2]interface{}{1, ' 2 '} var keyarraymap ma P[[2]interface{}]int = Make (Map[[2]interface{}]int) keyarraymap[keyarray] = 10000fmt. Println (Keyarraymap[keyarray])//Output 10000//interface{} type as key, but it contains the func () {} element, at which time the run times error keyArray2: = [2]interface{}{ 1, func () {}}//error message: Panic:runtime Error:hash of Unhashable type func () var keyArrayMap2 map[[2]interface{}]int = Make (ma P[[2]interface{}]int) Keyarraymap2[keyarray2] = 20000fmt. Println (Keyarraymap2[keyarray2])//Use a struct containing slice as key, because slice cannot be a key to the map, so run the times wrong var test2 map[person2]int = Make (Map[person2]int) test2[person2{"Fuqiu", []string{"1", "2"}] = 1000fmt. Println (test2[person2{"Fuqiu", []string{"1", "2"}])//error message: Invalid map key type Person2
- ValueType Description: Can be any type, by using the interface{} type, we can store any value
- You do not need to know the length of the map when declaring it, and map can grow dynamically.
- The value of the uninitialized map is nil
Map some points to be aware of
- Map is a reference type
- The key to the map is unordered (TODO: How to do it in order)
- Do not use new, always use make to construct the map, if you mistakenly use new () to assign a reference object, you will get a null reference pointer, equivalent to declare an uninitialized variable and take its address
mapCreated := new(map[string]float32)// 以下操作编译器会报错:invalid operation: mapCreated["key1"] (index of type *map[string]float32).mapCreated["key1"] = 4.5
Some tips for using map
- Map[string]bool to achieve the effect of set
- Map[string]func () {}
How map is implemented
Todo
Resources
- The-way-to-go
- Effective Go