Golang learning road-map

Basic introduction to map

Map is an unordered key-value-based data structure. Map in Go language is a reference type and must be initialized before it can be used.

map statement

1. Basic grammar

var 变量名 map[keytype]valuetype

Description:

  • The key of map in golang can be of many types, such as bool, string, pointer, channel, integer, floating point number, and it can also be an interface, structure, and array containing the previous types. Usually the key is int, string . Note: Slice, map and function are not allowed, because these few can't be judged by ==.
  • The type of valuetype is basically the same as that of key. Usually integer, floating point, string, map, struct.

2. Examples of map declarations

var a map[string]string
var a map[int]string
var a map[string]int
var a map[string]map[string]string
...

Note: The statement will not allocate memory. Initialization requires make, and the memory can be assigned and used only after the memory is allocated.

3. Case demonstration

package main

import(
	"fmt"
)

func main(){
    
    
	//map的声明和注意事项
	var a map[string]string
	//在使用map前,需要先make,make的作用就是给map分配数据空间
	a = make(map[string]string,10)
	a["no1"] = "Casey"
	a["no2"] = "Jerry"
	a["no3"] = "Jessical"
	a["no1"] = "Huhuhu"
	fmt.Println(a)
}

operation result:
Insert picture description here

Description:

  • Map must be made before use
  • The key of the map cannot be repeated. If it is repeated, the last key-value shall prevail.
  • The value of the map can be the same
  • The key-value of the map is unordered
  • make built-in functions
    Insert picture description here

Use of map

package main

import(
	"fmt"
)

func main(){
    
    
	//第一种使用方式
	var a map[string]string
	//在使用map前,需要先make,make的作用就是给map分配数据空间
	a = make(map[string]string,10)
	a["no1"] = "Casey"
	a["no2"] = "Jerry"
	a["no3"] = "Jessical"
	a["no1"] = "Huhuhu"
	fmt.Println(a)
	//第二种使用方式
	cities := make(map[string]string)
	cities["c1"] = "北京"
	cities["c2"] = "深圳"
	cities["c3"] = "广州"
	fmt.Println(cities)

	//第三种使用方式
	heroes := map[string]string{
    
    
		"hero1" : "宋江",
		"hero2" : "卢俊义",
		"hero3" : "武松",
	}
	heroes["hero4"] = "林冲"
	fmt.Println(heroes)
}

Operation result:
Insert picture description here
practice case:

package main

import(
	"fmt"
)

//案例
/* 
  演示一个key-value的value是map的案例
  比如:我们要存放学生的信息,每个学生有name,sex信息

*/
func main(){
    
    
	stuMap := make(map[string]map[string]string)
   
	stuMap["stu1"] = make(map[string]string)
	stuMap["stu1"]["name"] = "Casey"
	stuMap["stu1"]["sex"] = "女"

	stuMap["stu2"] = make(map[string]string)
	stuMap["stu2"]["name"] = "Jerry"
	stuMap["stu2"]["sex"] = "男"

	fmt.Println(stuMap)
	fmt.Println(stuMap["stu1"])
	fmt.Println(stuMap["stu2"]["name"])


}

operation result:
Insert picture description here

Add, delete, modify and check the map

1. Map increase and update

map[key] = value
If the key does not exist yet, it is added, and if the key exists, it is modified.

func main(){
    
    
	
	var a map[string]string
	
	a = make(map[string]string,10)
	a["no1"] = "Casey"
	a["no2"] = "Jerry"
	a["no3"] = "Jessical"
	a["no1"] = "Huhuhu"
	fmt.Println(a)
}

2. Map delete

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, but no error will be reported.
Insert picture description here
Case demonstration :

package main

import(
	"fmt"
)

//案例
/* 
  演示一个key-value的value是map的案例
  比如:我们要存放学生的信息,每个学生有name,sex信息

*/
func main(){
    
    
	
	var a map[string]string
	
	a = make(map[string]string,10)
	a["no1"] = "Casey"
	a["no2"] = "Jerry"
	a["no3"] = "Jessical"
	a["no1"] = "Huhuhu"
	fmt.Println(a)
	delete(a,"no1")
	fmt.Println(a)
}

Operation result:
Insert picture description here
Note:
If we want to delete all the keys in the map, there is no special method to delete them at once. You can traverse the keys and delete them one by one, or map = make(...), make a new one, and make the original one become rubbish. Reclaimed by gc.

3. Map search

val, key := a["no2"]
	if key {
    
    
		fmt.Printf("查找成功,值为%v\n",val)
	}else{
    
    
		fmt.Printf("没有找到...\n")
	}

Operation result:
Insert picture description here
**Description:** If there is "no2" in the map a, then it will return true, otherwise it will return false.

map traversal

The traversal of the map uses the for-range structure traversal.

package main

import(
	"fmt"
)

func main(){
    
    
	
	var a map[string]string
	
	a = make(map[string]string,10)
	a["no1"] = "Casey"
	a["no2"] = "Jerry"
	a["no3"] = "Jessical"
	a["no1"] = "Huhuhu"
	for key, value := range a{
    
    
		fmt.Printf("k = %v,v = %v\n",key,value)
	}
}

operation result:
Insert picture description here

Map usage details

  1. Map is a reference type, and it obeys the mechanism of application type transfer. After a function receives a map, after modification, it will directly modify the original map.
  2. After the capacity of the map is reached, if you want to add elements, it will automatically expand without panic, that is to say, after the map can dynamically increase the key-value pair (key-value) and
    change the capacity to 1, the program can still run normally.
func main(){
    
    
	
	var a map[string]string
	//j将容量改为1
	a = make(map[string]string,1)
	a["no1"] = "Casey"
	a["no2"] = "Jerry"
	a["no3"] = "Jessical"
	a["no1"] = "Huhuhu"
	for key, value := range a{
    
    
		fmt.Printf("k = %v,v = %v\n",key,value)
	}
}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44736475/article/details/114150263