Go language Mapping (map) usage Analysis _golang

Source: Internet
Author: User

This example describes the Go language map (map) usage. Share to everyone for your reference. Specifically as follows:

A map is a built-in data structure that holds the unordered collection of key-value pairs.

(1) Mapping creation

Make (map [KeyType] valuetype, initialcapacity)

Make (map [KeyType] valuetype)

Map [KeyType] ValueType {}

Map [KeyType] ValueType {key1:value1, key2:value2, ..., Keyn:valuen}

For example, create an array in 4 ways, where the difference between the first and second is that there is no initial capacity specified, but this is not necessary when used, because the nature of the map determines that it automatically expands once the capacity is insufficient:

Copy Code code as follows:
Func test1 () {
MAP1: = Make (map[string]string, 5)
MAP2: = Make (map[string]string)
MAP3: = map[string]string{}
MAP4: = map[string]string{"A": "1", "B": "2", "C": "3"}
Fmt. Println (Map1, MAP2, MAP3, MAP4)
}

The output is as follows:

Map[] map[] map[] map[c:3 a:1 B:2]

(2) Mapping of fills and traversal

Copy Code code as follows:
Func test2 () {
MAP1: = Make (map[string]string)
Map1["a"] = "1"
Map1["B"] = "2"
Map1["C"] = "3"
For key, Value: = Range Map1 {
Fmt. Printf ("%s->%-10s", key, value)
}
}

As above, the array's padding uses map[key] = value, and when traversing the mapping, each item returns 2 values, the key, and the value. The results are as follows:

A->1 b->2 c->3

(3) Mapping lookup, modification, and deletion

Copy Code code as follows:
Func test3 () {
MAP4: = map[string]string{"A": "1", "B": "2", "C": "3"}
Val, exist: = map4["a"]
Val2, Exist2: = map4["D"]
Fmt. Printf ("%v,%v\n", exist, Val)
Fmt. Printf ("%v,%v\n", Exist2, Val2)

Map4["a"] = "8"//Modify mappings and add mappings no difference
Fmt. Printf ("%v\n", MAP4)

Fmt. Println ("Delete B:")
Delete (MAP4, "B")
Fmt. Printf ("%v", MAP4)
}

When the map specifies that the key takes the corresponding value, you can specify that two values be returned, the first is the corresponding value, and the second is a bool to indicate whether there is a value. As above, "a" must have a value, "B" certainly has no value.

modifying mappings and adding mappings is no different, if the specified key does not exist, create it, otherwise, modify it.

Delete is using the Go's built-in function delete, and the output is as follows:

true,1
False
Map[a:8 B:2 C:3]
Delete B:
Map[a:8 C:3]

I hope this article will help you with your go language program.

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.