map
map is an unordered collection of key-value pairs.
The most important point of map is to quickly retrieve data through keys. Keys are similar to indexes and point to the value of data.
Map is a collection, so we can iterate over it like iterating over arrays and slices. However, maps are unordered and we cannot decide the order of return, because maps are implemented using hash tables.
Map is a reference type and can be declared in the following way:
// There are spaces allowed between [keytype] and valuetype.var mapname map[keytype]valuetype
in:
- mapname is the variable name of map.
- keytype is key type.
- The valuetype is the value type corresponding to the key.
Notice
When declaring, you do not need to know the length of the map, because maps can grow dynamically.The value of the uninitialized map is nil, use functions
len()
You can get the number of key-value pairs in the map.
Here are two examples:
Example 1:
package main import ( "fmt" ) func main() { //Initialize a map without key-value pairs map1 := map[int]int{} //falsae (map1 == nil) //No initialization var map2 map[int]int //true (map2 == nil) }
Example 2:
package main import "fmt" func main() { var mapLit map[string]int var mapAssigned map[string]int //Initialize a map with two key-value pairs mapLit = map[string]int{"one": 1, "two": 2} mapAssigned = mapLit mapAssigned["two"] = 3 ("Map literal at \"one\" is: %d\n", mapLit["one"]) ("Map assigned at \"two\" is: %d\n", mapLit["two"]) ("Map literal at \"ten\" is: %d\n", mapLit["ten"]) }
What is the output result?
The output result is
Map literal at "one" is: 1
Map assigned at "two" is: 3
Map literal at "ten" is: 0
Because mapAssigned is a reference to mapList, modifications to mapAssigned will also affect the value of mapList. Therefore, when mapAssigned["two"] is modified to 3, mapList["two] is also 3.
There is another way to create map
make(map[keytype]valuetype,cap)
For example:
map2 := make(map[string]int, 100)
When the map grows to the upper capacity limit, if a new key-value is added, the size of the map will automatically be increased by 1. Therefore, for performance reasons, for large maps or maps that will expand rapidly, it is best to indicate it first even if you only know the capacity roughly.
Since a key can only correspond to one value, and value is a primitive type, what if a key needs to correspond to multiple values?
The answer is: Use slices
For example, when we want to process all processes on a unix machine, the parent process (pid is shaped) is used as the key, and all child processes (slices composed of pids of all child processes) are used as the value.
This problem can be solved gracefully by defining value as []int type or other types of slices. The sample code is as follows:
mp1 := make(map[int][]int) mp2 := make(map[int]*[]int)
Supplement: Why is map output unordered?
When traversing the map, take a random number and randomize the traversal order of the bucket. The reason is that the underlying golang does not guarantee this, and perhaps (now/afterwards) there will be special situations where the order is not fixed. Worrying that developers misunderstand this point, golang deliberately disrupted this order, letting developers know that the underlying golang does not guarantee that maps will be in the same order every time they traverse.
Go's Map is essentially "disordered"
"Unordered" write:
- Normal write (non-hash conflict write): It is hashing to a certain bucket, not writing in buckets order.
- Hash conflict writing: If there is a hash conflict, it will be written to the same bucket, and it is more likely to be written to the overflow bucket.
Expansion leads to disorder
- External expansion forces elements to change in order, while equal expansion does not change the order of elements
Summary of the causes of disorder
- Unordered writing
- Extended expansion forces elements to change order
Summarize
This is the article about the statement and initialization of Go learning notes. For more information about Go maps, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!