1. Map
Map is an unordered key-value based data structure. Map in GO language is a reference type and must be initialized with Make before it can be used.
1.1.1 map definition
The definition syntax of map in GO language is as follows
map[KeyType]ValueType
in,
KeyType: Indicates the type of key.
ValueType: Indicates the type of the value corresponding to the key.
The default initial value of the map type variable is nil, and the make() function needs to be used to allocate memory. The syntax is:
make(map[KeyType]ValueType, [cap])
Where cap represents the capacity of the map. Although this parameter is not necessary, we should specify a suitable capacity for it when initializing the map.
1.1.2 Basic use of map
The data in the map all appear in pairs. The basic example code of the map is as follows:
func main() {
scoreMap := make(map[string]int, 8)
scoreMap["Zhang San"] = 90
scoreMap["Xiao Ming"] = 100
fmt.Println(scoreMap)
fmt.Println(scoreMap["Xiao Ming"])
fmt.Printf("type of a:%T\n", scoreMap)
}
output:
map[Xiao Ming:100 Zhang San:90]
100
type of a:map[string]int
map also supports filling elements at the time of declaration, for example:
func main() {
userInfo := map[string]string{
"username": "pprof.cn",
"password": "123456",
}
fmt.Println(userInfo) //
}
1.1.3 Determine whether a key exists
In the Go language, there is a special way to judge whether the key in the map exists. The format is as follows:
value, ok := map[key]
for example:
func main() {
scoreMap := make(map[string]int)
scoreMap["Zhang San"] = 90
scoreMap["Xiao Ming"] = 100
// If the key exists ok is true, v is the corresponding value; no ok is false, v is the zero value of the value type
v, ok := scoreMap["Zhang San"]
if ok {
fmt.Println(v)
} else {
fmt.Println("No such person found")
}
}
1.1.4 Traversal of map
Go language uses for range to traverse map.
func main() {
scoreMap := make(map[string]int)
scoreMap["Zhang San"] = 90
scoreMap["Xiao Ming"] = 100
scoreMap["Wang Wu"] = 60
for k, v := range scoreMap {
fmt.Println(k, v)
}
}
But when we just want to traverse the key, we can write it as follows:
func main() {
scoreMap := make(map[string]int)
scoreMap["Zhang San"] = 90
scoreMap["Xiao Ming"] = 100
scoreMap["Wang Wu"] = 60
for k := range scoreMap {
fmt.Println(k)
}
}
Note: The order of elements when traversing the map is independent of the order in which key-value pairs are added.
delete(map, key)
in,
map: a map representing key-value pairs to delete
key: represents the key of the key-value pair to delete
The sample code is as follows:
func main(){
scoreMap := make(map[string]int)
scoreMap["Zhang San"] = 90
scoreMap["Xiao Ming"] = 100
scoreMap["Wang Wu"] = 60
delete(scoreMap, "Xiao Ming")//Delete Xiao Ming: 100 from the map
for k,v := range scoreMap{
fmt.Println(k, v)
}
}
1.1.6 Traverse map in specified order
func main() {
rand.Seed(time.Now().UnixNano()) //Initialize random number seed
var scoreMap = make(map[string]int, 200)
for i := 0; i < 100; i++ {
key := fmt.Sprintf("stu%02d", i) //Generate a string starting with stu
value := rand.Intn(100) //Generate a random integer from 0 to 99
scoreMap[key] = value
}
// Take out all the keys in the map and store them in the slice keys
var keys = make([]string, 0, 200)
for key := range scoreMap {
keys = append(keys, key)
}
//sort the slices
sort.Strings(keys)
// Traverse map by sorted key
for _, key := range keys {
fmt.Println(key, scoreMap[key])
}
}
1.1.7 Elements are slices of type map
The following code demonstrates the operation when the elements in the slice are of type map:
func main() {
var mapSlice = make([]map[string]string, 3)
for index, value := range mapSlice {
fmt.Printf("index:%d value:%v\n", index, value)
}
fmt.Println("after init")
// Initialize the map elements in the slice
mapSlice[0] = make(map[string]string, 10)
mapSlice[0]["name"] = "Wang Wu"
mapSlice[0]["password"] = "123456"
mapSlice[0]["address"] = "Red Flag Street"
for index, value := range mapSlice {
fmt.Printf("index:%d value:%v\n", index, value)
}
}
1.1.8 map whose value is a slice type
The following code demonstrates the operation of the slice type in the map:
func main() {
var sliceMap = make(map[string][]string, 3)
fmt.Println(sliceMap)
fmt.Println("after init")
key := "China"
value, ok := sliceMap[key]
if !ok {
value = make([]string, 0, 2)
}
value = append(value, "Beijing", "Shanghai")
sliceMap[key] = value
fmt.Println(sliceMap)
}