Go language Learning Note 13: Map Collection

Source: Internet
Author: User

Go language Learning Note 13: Map Collection

Map is basically in each language, Java is a collection class map, which includes HashMap, TreeMap and so on. The Python language is directly a type that is simpler than Java.
Map in the go language is simpler than Java, and more cumbersome than python.

Define Map
var x map[string]stringx : = make(map[string]string)

There are some strange ways to do this, the map is the keyword, the right-hand bracket is the type of key, and the outer bracket is the type of value. In general, a comma or colon is used to separate key and value, but the go language does not follow this principle, but instead uses parentheses and parentheses.
And the map must be initialized, otherwise it will become nil map, and nil map cannot be used to store key-value pairs.

package mainimport "fmt"func main() {    var x map[string]string    x = make(map[string]string)        x["a"] = "1";    x["b"] = "2";        for item := range x {        fmt.Println(item, x[item]);    }        value, exist := x["a"]    if (exist) {        fmt.Println("x has", value);    }}
Delete function

The delete () function is used to delete the elements of the collection, with the parameter map and its corresponding key.

fruits := map[string]string {"apple": "12", "orange": "210"}delete(fruits, "apple")

Go language Learning Note 13: Map Collection

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.