Above: juejin. Cn/post / 695359…

4. Delete map elements

delete(map, key)
// Delete the key pair corresponding to "blue sky and white Cloud" in M1
delete(m1,"Blue sky and white Clouds")
Copy the code

5. Traverse the Map elements

There is traversal of the entire map key-value pairs, and traversal of the key and value pairs individually.

package  main
import "fmt"
//map
 
func main(a){
	var m1 map[string] int
	m1 = make(map[string]int.10)
	m1["Blue sky and white Clouds"] = 4
	m1["One more."] = 4
  
	/ / the map of traversal
	fmt.Println("Map traversal :")
	for key,value := range m1{
		fmt.Println(key,value)
	}
	// just iterate over the key
	fmt.Println("Only iterate over key:")
	for key := range m1{
		fmt.Println(key)
	}
	// Just iterate over value
	fmt.Println("Just iterate over value:")
	for _,value := range m1{
		fmt.Println(value)
	}
}
Copy the code

Output result:

Map traversal: blue sky and white cloud 4 and 4 traversal key: blue sky and white cloud and one value: 4 and 4Copy the code

Note: Duplicate values are iterated over.

6. Use slices to traverse the map in the specified order

Steps:

  1. Take out all the key values in the map and store them in the slice
  2. Sort the slice values by using the function sort()
  3. Finally, the map is traversed by the ordered keys
package  main
import "fmt"
import "math/rand"
import "sort"
import "time"

func main(a) {
	// Initializes the random number seed
	// time.now ().UnixNano gets the current time in nanoseconds
	rand.Seed(time.Now().UnixNano()) 

	var scoreMap = make(map[string]int.10)

	for i := 0; i < 10; i++ {
		key := fmt.Sprintf("stu%02d", i) // Generate a string beginning with stu
		value := rand.Intn(1000)          // Generate a random integer from 0 to 999
		scoreMap[key] = value
	}

	fmt.Println(scoreMap)
	// Remove all keys from map and store them in slice keys
	var keys = make([]string.0.10)
	for key := range scoreMap {
		keys = append(keys, key)
	}
	// Call sort () to sort slices
	sort.Strings(keys)
	// Traverses the map according to the sorted key
	for _, key := range keys {
		fmt.Println(key, scoreMap[key])
	}
}
Copy the code

Output result:

map[stu00:484 stu01:283 stu02:81 stu03:198 stu04:267 stu05:572 stu06:646 stu07:442 stu08:674 stu09:55]
stu00 484
stu01 283
stu02 81
stu03 198
stu04 267
stu05 572
stu06 646
stu07 442
stu08 674
stu09 55
Copy the code

7. Obtain the map median value and check whether it exists

V, exists := m1["Green Meadow"]
fmt.Printf("%d %t",v,exists)
Copy the code

Output:

7 true
Copy the code

8. Slice elements are of map type

Note:

In addition to initializing Slice, map is initialized as well

package  main
import "fmt"

func main(a) {
	// The element is a slice of map type, the slice size is 10, the capacity is 10, the map index is string, the value is int
	var slice = make([]map[int]string.10.10)
	// Initialize the internal map
	slice[0] = make(map[int]string.1)
	slice[0] [0] = "Wash and brush"
	slice[1] = make(map[int]string.2)
	slice[1] [0] = "Wash away."
	slice[1] [1] = "Little Grey Rabbit"
	fmt.Println(slice)
}
Copy the code

Output result:

[map [0: wash brush] map [0: wash white white 1: little gray rabbit] map map map map [] [] [] [] map map map map [] [] [] []]Copy the code

9. Map elements are of slice type

package  main
import "fmt"

// The map value is slice
func main(a) {
	//map[int][]string
	var m = make(map[int] []string.10)
	//[[]string{" I am at home "," sleep every day "," happy "} initializes the string type slice
	m[0] = []string{"I'm at home."."Sleep every day."."What a joy!"}
	fmt.Println(m)
}
Copy the code

Output result:

Map [0:[I sleep at home every day happy ah]]Copy the code

For slicing some forget, welcome to look at the slicing summary I wrote before, there is everything about the operation of slicing ~

Juejin. Cn/post / 695126…

Map complete!! And flowers!