Learning Golang Language (7): Type--Dictionary

Source: Internet
Author: User
Tags learn golang

follow the code to learn Golang language. Today we are studying the built-in data structure of the Go language: A dictionary (map).

----------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------

dictionary (MAP)

A dictionary (map) is a data structure built into the go language, a set of unordered sets of key-value pairs.

Some places are called mappings. All of this is a dictionary (map).

A dictionary (map) is also called an associative array. Because the array looks up the elements by index, the dictionary finds the elements by key (key).

The capacity of the dictionary (map) is limited only by memory. In a dictionary, all keys are unique and must support = = and. = The type of the operator. If you attempt to assign a value to the same key, the value of the assigned value will overwrite the original value.


map creation and fill

There are two types of map definitions:

1, the initialization of the data definition method.

2. Use the Make function to define.

Output Result:

Map literal at "one" is:1

Map created at "Key2" is:3.141590

Map assigned at "Is:3"

Map literal at "ten" is:0

Where Maplit use method is justified, and mapcreated uses method two.

---------------------------------------------

The following points need to be noted in the use of map:

1, map is unordered, each print out of the map will not be the same, it can not be obtained through index, and must be obtained through key;

2, the length of map is not fixed. Like slice, map is also a reference type;

3. The built-in Len function also applies to the map, returning the number of keys the map has;

4, the value of map can be easily modified.


access to map

When the key to the access element exists in the dictionary, there is no problem and the corresponding element can be obtained. But if it doesn't exist.

This will return a value of 0, for a string of 0 value is "", for an integer 0 value is 0.

But there is a situation like this:

This time will output: 0. But this is confused with the value corresponding to the a key value. Go provides a way to solve this problem.

The use of x["E"] has two return values at this time. One is the value, a bool variable that has the key.

That is

_, OK: = Map1[key1]//OK = = True if Key1 is present, false otherwise


Delete element of map

The go language provides a built-in function delete, which can be used to remove elements from the dictionary.

Output Result:

Before deleting an element, the map length is 4, and the length after deletion is 3.< here use the Len function to get the length of the map >.

In addition, if you try to delete a key that does not exist in the map, the program will not error. It just doesn't have any effect on the map.


For...range Structure

---------------------------------------------

The For...range structure can be used for map.


For key, Value: = Range Map1 {

Do something

}

The first return value is the key (key), and the second return value is the value (value). These two values are local variables that act only on the inside of the For loop. For example:

Output Result:


Here we can also see that the map is unordered.

---------------------------------------------

If you only care about values (value), you can use this:

For _, Value: = Range Map1 {

Do something

}

Output Result:

---------------------------------------------

If you only care about keys, you can use this:

For key: = Range Map1 {

Fmt. Printf ("Key is:%d\n", key)

}

Output Result:

--------------------Summary--------------------

So far we have learned all the built-in types of the go language.

Includes: Boolean, numeric, string, array, slice, and map.

---------------------------------------------

Welcome to focus on the code of operation. Learn Golang language together.



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.