This is a creation in Article, where the information may have evolved or changed.
Listen to the complex types in the Go language in addition to arrays, slices, there is a map, say map estimate everyone is not unfamiliar, because in Java, C + + and other languages have its figure, it in the form of <key,value> for programmers to provide services.
You can also see that six city information is stored in memory, where key is the city telephone area code and value is the city name. The City telephone area code (key) is unique, so that the programmer to the city name (value) to increase, delete, change, check and other operations; There is also a certain association between <key,value>, that is, the arrow in the diagram, But this association does not need special attention to the beginner of Go language.
First, map declaration, initialization and assignment
1. Declaration format: var variable name map[key value type]value value type
For example:
var cityMap map[string]string
var employeemap map[int]string
var boolmap map[string]bool
2. Map initialization
var cityMap map[string]string = map[string]string{"010": "Beijing", "022": "Tianjin", "021": "Shanghai", "020": "Guangzhou", "0755": "Shenzhen", "0571" : "Hangzhou"}
var employeemap map[int]string = map[int]string{1: "Boss", 2: "Manager", 3: "Employee"}
var boolmap map[string]bool = map[string]bool{"North Korea": True, "Japan": false, "China": true, "Russia": false}
Of course, if this sentence is too long, it can be simplified as follows:
var cityMap = map[string]string{"010": "Beijing", "022": "Tianjin", "021": "Shanghai", "020": "Guangzhou", "0755": "Shenzhen", "0571": "Hangzhou"}
var employeemap = map[int]string{1: "Boss", 2: "Manager", 3: "Employee"}
var boolmap = map[string]bool{"North Korea": True, "Japan": false, "China": true, "Russia": false}
This can also be simplified:
CityMap: = map[string]string{"010": "Beijing", "022": "Tianjin", "021": "Shanghai", "020": "Guangzhou", "0755": "Shenzhen", "0571": "Hangzhou"}
Employeemap: = map[int]string{1: "Boss", 2: "Manager", 3: "Employee"}
Boolmap: = map[string]bool{"North Korea": True, "Japan": false, "China": true, "Russia": false}
If the above two simplification methods or feel too long, you can use the method of assignment
3. Map Assignment
var cityMap map[string]string hear //DECLARE CITYMAP variable
var employeemap map[int]string hear //DECLARE EMPLOYEEMAP variable
var boolmap map[string]bool hear //DECLARE BOOLMAP variables
CityMap = map[string]string{} Listen //Initialize variable CityMap
citymap["010" = "Beijing" Listen ///Assign value to variable CityMap
citymap["022"] = "Tianjin"
citymap["021"] = "Shanghai"
Citymap["020"] = "Guangzhou"
citymap["0755"] = "Shenzhen"
citymap["0571" = "Hangzhou"
Employeemap = map[int]string{} Listen //Initialize variable Employeemap
EMPLOYEEMAP[1] = "Boss" Listen //Assign value to variable Employeemap
EMPLOYEEMAP[2] = "Manager"
EMPLOYEEMAP[3] = "Employee"
Boolmap = map[string]bool{} Listen //Initialize variable Boolmap
boolmap["North Korea"] = True Listen and listen //Assign values to variable Boolmap
Boolmap["Japan"] = False
boolmap["China"] = True
Boolmap["Russia"] = False
Ii. creating and traversing a map
1. Create a map with make
In addition to declaring the map in the way above, you can create a new map like slice with the go language built-in function make (). The above example can be rewritten as follows:
CityMap: = Make (map[string]string)
Employeemap: = Make (map[int]string)
Boolmap: = Make (Map[string]bool)
Map creation is similar to the creation of slice, and can also specify the capacity of the map at creation time
CityMap: = Make (map[string]string, 6)
Employeemap: = Make (map[int]string, 3)
Boolmap: = Make (Map[string]bool, 5)
2. Traverse Map
Here's an example of how to traverse a map: (1) Create a map and assign a value (2) traverse map (3) Delete a city with a telephone area code of 022
Create a map and assign a value
CityMap: = Make (map[string]string, 6)
citymap["010" = "Beijing"
citymap["022"] = "Tianjin"
citymap["021"] = "Shanghai"
Citymap["020"] = "Guangzhou"
citymap["0755"] = "Shenzhen"
citymap["0571" = "Hangzhou"
Fmt. Println (CITYMAP)
Traverse Map
For k, _: = Range CityMap {
Hear//delete a city with a telephone area code of 022
Listen if k = = "022" {
Listen and listen to the Delete (CityMap, k)
Listen}
}
Fmt. Println (CITYMAP)
The result of the program operation is:
3. Multiple return values
Listening to the Go language and Java, C + +, a big difference is that: multiple return values , go language functions can have multiple return values, this feature makes code writing more concise
For key, Value: = Range citymap{
Listen//slightly
}
When using Traverse Citymap, the <key,value> value between the map is returned, the programmer can use the key value, or use value, assuming the programmer does not need value, you can change the value to _ as in the example above.
For key, _ Listen: = Range citymap{
Listen//slightly
}
Third, map considerations
1. Map stores data in <key, value> form, where key must be a type that supports the = = or! = operation, not a map, slice, or function
2, map lookup faster than linear search speed
3. Determine the number of map elements using the Len () function instead of size ()
4, go language use the built-in delete () function to delete the elements in the map, the first parameter is a map, the second parameter is a key in the map, if the key does not exist, then nothing happens, but if the key is set to nil, then the deletion will throw an exception
Iv. value in Map
The value type in map can be either an underlying type or a composite type, or it can be a struct
1, the value type is the structure body
"Value type is a storage model for structs":
"Program Implementation":
Define Structure body
Type persion struct {
Listen to Name, Phone string
Listen to age listen to listen int
}
Creates a map whose value type is persion
Persionmap: = Make (Map[string]persion)
Assigns a value to the element in map whose key type is a string and the value type is persion
persionmap["123456"] = persion{name: "Corrected brother", Age:35, Phone: "13581887557"}
persionmap["789012"] = Persion{name: "Bare running brother", Age:5, Phone: "None"}
Traverse map to print out persion information
For _, Value: = Range Persionmap {
Listen to the FMT. Println ("Name:", value.) Name, "\ t Age:", value. Age, "\ t phone:", value. Phone)
}
"Run Results":
2. The value type is map
"Program Implementation":
Create a map whose value type is map
Persionmap: = Make (map[string]map[string]string)
persionmap["123456"] = map[string]string{"name": "Corrected brother", "phone": "13581887557", "Age": "35"}
persionmap["789012"] = map[string]string{"name": "Bare running brother", "phone": "None", "Age": "5"}
Traverse map to print out persion information
For _, Value: = Range Persionmap {
Listen to the FMT. Println ("Name:", value["name"], "\ t Age:", value["ages"], "\ t phone:", value["phone"])
}
"Run Results":
"description":
When the Vlaue type is map, it is similar to the previous value type of struct persion, except that the value of the health value pair is a map, but if you do not find the difference, the map appears as generics, as in this example, although the value of age is integer, but because of the generic requirements, The age here must be enclosed in double quotes.
This article is from the "Green Guest" blog, please be sure to keep this source http://qingkechina.blog.51cto.com/5552198/1620429