Golang Study notes -1.14 Maps

Source: Internet
Author: User

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

makeYou 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.