This is the 14th chapter of Golang Language Learning Tutorial
What is maps?
A map is a built-in type that associates a key (key) with a value in go. The value can be obtained by the corresponding key.
Create a map
make
You can create a map by passing in the type of key and value to the function. make(map[type of key]type of value)
is the syntax for creating a map.
money := make(map[string]int)
The above creates a map named, where the type of the money
key is string
, the type of the value is int
.
Map initial value is empty, need to add element to map
Adding elements to a map
The syntax and arrays for adding elements to a map are the same.
Example 1:
package mainimport "fmt"func main() { money := make(map[string]int) //创建 一个名为 money 的 map money["xunk"] = 20000 money["ergou"] = 30000 money["jobs"] = 40000 //给 money 添加元素 fmt.Println("money's map is ", money)}
The above program output is:money's map is map[xunk:20000 ergou:30000 jobs:40000]
You can also initialize the map at the time of declaration.
Example 2:
package mainimport "fmt"func main() { money := map[string]int { "xunk" : 2000, "ergou" : 3000, "jobs" : 4000, } money["bill"] = 6000 fmt.Println("money's map is ", money)}
The above program adds three elements when declaring a map, and then adds Bill, which outputs:
money's map is map[xunk:2000 ergou:3000 jobs:4000 bill:6000]
Keys are not necessarily only string types. All comparable types, such as boolean,interger,float,complex,string, can be used as keys.
Get the elements in a map
The element syntax for getting map is:map[key]
package mainimport "fmt"//make(map(type of key)type of value)func main() { money := map[string]int { //声明时添加值 "xunk" : 2000, "ergou" : 3000, "jobs" : 4000, } money["bill"] = 6000 name := "jobs" fmt.Println("the money of ", name, "is ", money[name])}
The above procedure obtains the amount of jobs and prints them out. The program output is:the money of jobs is 4000
If you get a nonexistent element, map returns a 0 value of the element for that type. As in the procedure above, we get a nonexistent key that returns int
the value of the type 0
.
money := map[string]int { //声明时添加值 "xunk" : 2000, "ergou" : 3000, "jobs" : 4000, } money["bill"] = 6000 fmt.Println("the money of danny is ", money["danny"])}
The program output is:the money of danny is 0
To see if the key exists in the map, the syntax is:value, ok := map[key]
money := map[string]int { //声明时添加值 "xunk" : 2000, "ergou" : 3000, "jobs" : 4000, } money["bill"] = 6000 newname := "jenny" value, ok := money[newname] if ok == true { fmt.Println("jenny's money is ", value) } else { fmt.Println(newname,"not found") }}
In the above program, if ok
= True, it means that the key exists, the corresponding value is value
, and vice versa does not exist.
The program output is:
jenny not found
Iterating through the elements in the map requires a for range
loop:
for key, value := range money{ fmt.Printf("map[%s] = %d\n", key, value) }
The above program output is:
Map[xunk] = 2000
Map[ergou] = 3000
Map[jobs] = 4000
Map[bill] = 6000
Delete an element in a map
Deleting the element syntax in map is: delete(map,key)
This function does not return a value.
book := map[string]int { "bainiangudu" : 3, "fooled" : 2, "blockchain" : 1, } delete(book, "bainiangudu") fmt.Println(book)}
The above program removed the key bainiangudu
, the program output is:
map[fooled:2 blockchain:1]
Map is a reference type
Like slice, map is also a reference type, and when a map is assigned a new variable, they point to the same internal data structure, and when one of the variables is changed, it affects the other.
newmoney := money newmoney["xunk"] = 5000 fmt.Println(money["xunk"])
In the above program, the value will be assigned to money
newmoney
, then change newmoney
the value in xunk
, then money
the xunk
values also change. The program output is:
5000
The above for learning Golang Maps Chapter