The use of Go language map
1, Basic introduction to map:
map is a key value data structure, also known as a field or associated array. (similar to the dictionary in Python)
Basic syntax:
Var map variable name map[keyType]valueType
- Generally, the key types are: int, string, or bool, number, pointer, channel, interface, structure, array;
- Generally, the types of value are: number (integer, floating-point), string, map, struct;
Case of map declaration:
var variable name map[string]string / / the key type of variable name is: string value type: String
var variable name map[string]map[string]string / / variable name key type: string value type: Map
- Note: the declaration will not allocate memory. make is required for initialization and can only be assigned and used after memory allocation.
Here's a chestnut:
package main import "fmt" func main() { // Declaration and notes of map // Note: before using map, you need to make and allocate data space to map myMap := make(map[string]string, 10) // 10 represents the space allocated to the map myMap["num1"] = "Asia rope" myMap["num2"] = "Ti Mo" myMap["num3"] = "Male knife" fmt.Println("myMap:", myMap) } // Output results: myMap: map[num1:Asia rope num2:Ti Mo num3:Male knife]
- map must be made before use;
- The map key cannot be repeated. If it is repeated, the latter will overwrite the former;
- The value of map can be repeated;
- The key value of map is unordered;
2, How to use map:
-
Method 1:
package main import "fmt" func main() { // First way of use var myMap map[string]string // Before using map, you need to make it first. The function of make is to allocate data space to map myMap = make(map[string]string) myMap["num1"] = "Asia rope" myMap["num2"] = "Ti Mo" myMap["num3"] = "Male knife" fmt.Println("myMap:", myMap) } // Output results: myMap: map[num1:Asia rope num2:Ti Mo num3:Male knife]
-
Mode two:
package main import "fmt" func main() { // The second way myMap := make(map[string]string) myMap["num1"] = "Asia rope" myMap["num2"] = "Ti Mo" myMap["num3"] = "Male knife" fmt.Println("myMap:", myMap) } // Output results: myMap: map[num1:Asia rope num2:Ti Mo num3:Male knife]
-
Mode three:
package main import "fmt" func main() { // The third way myMap := map[string]string{ "num1": "Asia rope", "num2": "Ti Mo", "num3": "Male knife", } fmt.Println("myMap:", myMap) } // Output results: myMap: map[num1:Asia rope num2:Ti Mo num3:Male knife]
Here's a chestnut:
package main import "fmt" func main() { /* For example, to store three student information, each student has the information of name and sex Idea: map[string]map[string]string */ // Pay attention to the use of make studentsMap := make(map[string]map[string]string) studentsMap["num1"] = make(map[string]string, 3) studentsMap["num1"]["name"] = "Asia rope" studentsMap["num1"]["sex"] = "male" studentsMap["num1"]["address"] = "Canyon top" studentsMap["num2"] = make(map[string]string, 3) studentsMap["num2"]["name"] = "Ti Mo" studentsMap["num2"]["sex"] = "secrecy" studentsMap["num2"]["address"] = "Canyon top" fmt.Println("studentsMap:", studentsMap) } // Output results: studentsMap: map[num1:map[address:Canyon top name:Asia rope sex:male] num2:map[address:Canyon top name:Ti Mo sex:secrecy]]
3, Add, delete, modify and check map:
-
map addition and update:
package main import "fmt" func main() { // map increase myMap := make(map[string]string) myMap["name"] = "Asia rope" myMap["sex"] = "male" // map change myMap["name"] = "Asia rope" myMap["sex"] = "secrecy" fmt.Println("myMap:", myMap) } // Output results: myMap: map[name:Asia rope sex:secrecy]
If the key does not exist, it is increased. If the key already exists, it is modified
-
map delete:
Basic syntax: delete(map, "key"); delete is a built-in function
- If the key exists, delete the key value;
- If the key does not exist, no operation and no error will be reported;
package main import "fmt" func main() { // Increase of map myMap := make(map[string]string) myMap["name"] = "Asia rope" myMap["sex"] = "male" // Delete existing key delete(myMap, "sex") // Delete the key that does not exist. No error or error will be reported when deleting delete(myMap, "address") fmt.Println("myMap:", myMap) } // Output results: myMap: map[name:Asia rope]
-
map lookup:
package main import "fmt" func main() { // map search myMap := make(map[string]string) myMap["name"] = "Asia rope" myMap["sex"] = "male" val, got := myMap["name"] if got { fmt.Println("The value is:", val) }else { fmt.Println("No value!") } } // Output results: //The value is: Asia rope
If the key in my map is "name", then get will return true, otherwise false
4, map traversal:
map traversal can only use for range
package main import ( "fmt" ) func main() { // Use of map for range myMap := make(map[string]map[string]string) myMap["num1"] = make(map[string]string) myMap["num1"]["name"] = "Asia rope" myMap["num1"]["sex"] = "male" myMap["num2"] = make(map[string]string) myMap["num2"]["name"] = "Male knife" myMap["num2"]["sex"] = "male" for k, v := range myMap{ fmt.Println("k=", k) for k1, v1 := range v{ fmt.Printf("\t k1=%v v1=%v", k1, v1) } fmt.Println() } } // Output results: k= num2 k1=name v1=Male knife k1=sex v1=male k= num1 k1=name v1=Asia rope k1=sex v1=male
5, map sort:
- There is no special method to sort map in go;
- map in go is unordered by default;
- map sorting in go is to sort the key first, and then traverse the output according to the key value;
Here's a chestnut:
package main import ( "fmt" "sort" ) func main() { // Sort the output according to the key sequence of the map // 1. First put the map key into the slice // 2. Slice operation // 3. Traverse the slice and output the map value according to the key mySort := make(map[int]int) mySort[1] = 100 mySort[3] = 66 mySort[2] = 88 mySort[5] = 16 fmt.Println("mySort=", mySort) var keys []int for k, _ := range mySort { keys = append(keys, k) } fmt.Println("keys", keys) // Sort slices sort.Ints(keys) fmt.Println("keys", keys) for _, v := range keys{ fmt.Printf("mySort[%v]=%v\n", v, mySort[v]) } } // Output results: mySort= map[1:100 2:88 3:66 5:16] keys [3 2 5 1] //After sorting=keys [1 2 3 5] mySort[1]=100 mySort[2]=88 mySort[3]=66 mySort[5]=16
6, map usage details:
-
map is a reference type, which follows the reference type passing mechanism:
package main import "fmt" func test (map1 map[int]int) { map1[10] = 600 } func main() { // map is a reference type myMap := make(map[int]int) myMap[10] = 1000 myMap[1] = 1000 test(myMap) fmt.Println("myMap value:", myMap) } // Output results: myMap value: map[1:1000 10:600]
-
value in map. The data structure is usually of struct type:
package main import "fmt" //Defining a student's structure type Stu struct { Name string Age int Address string } func main() { //map value, often using struct type students := make(map[string]Stu) //Create 2 students Stu1: = stu {"Tom", 18, "Beijing"} Stu2: = stu {"Mary", 20, "Shanghai"} students["num1"] = stu1 students["num2"] = stu2 fmt.Println(students) //Traverse to get each student's information for k, v := range students { fmt.Println("student number is:", k) fmt.Println("student's name is:", v.Name) fmt.Println("student's age is:", v.Age) fmt.Println("student's address is:", v.Address) } fmt.Println() } //Output results: map[num1:{Tom 18 Beijing} num2:{mary 20 Shanghai}] Student number: num1 The student's name is Tom The age of the student is: 18 The student's address is: Beijing Student number: num2 The student's name is mary The age of the student is: 20 The student's address is: Shanghai