Go Language Learning Road four: Dictionaries

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Associative array: (hash or dictionary)

Map is the go built-in correlation data type, the dictionary is accessed by Key to Value , the access format is as follows:

Value=mapname[key]

In fact, the array can be regarded as a dictionary of key-value type integer, which can be said that the array is a special dictionary.

1. Dictionary Item Lookup

To find a specific key-value pair from the dictionary, you can:

V,ok:=mapname[key]

After executing this statement, if the key value of the lookup exists, assign the value of the key corresponding to v, OK to be true , conversely, v equals 0 , OK bit false . Test Case:

var map1 = map[string]int{"Key1": +, "Key2": 200}

v,ok:=map1["Key1"]

If ok{

Fmt. Println (V,ok)

}else{

Fmt,println (v)

}

2. Deletion and addition of dictionaries

The code example is shown below:

var map1 = map[string]int{"Key1": 1, "Key2": 2, "Key3": 3}

For k, V: = Range Map1 {

Fmt. Println (k, v)

if k = = "Key1" {

Delete (Map1, k)

}

if k = = "Key3" {

map1["Key2"] = 5

}

}

Fmt. Println (MAP1)

Operation Result:

Key1 1

Key2 2

Key3 3

Map[key3:3 Key2:5]

The dictionary is relatively simple, do not spend too much ink to repeat the

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.