Go language learning 6-dictionary type

3. Go language data types

In the previous article, we introduced the slice type of the Go language. In this article, we will learn about the dictionary type of the Go language.

3.4 Dictionaries

In the Go language, the official name of the dictionary type is Map, which is an implementation of Hash Table.

3.4.1 Type notation

If the key type of a dictionary type is K and the element type is T, then the type literal used to represent the dictionary type:

map[K]T

The element type in the dictionary type declaration can be any valid Go language data type (except function type, dictionary type or slice type). The types of keys must be comparable. If the key type of the dictionary type is an interface type, then it is required that the dynamic type of each key value in the dictionary value of this type must be comparable during the running of the program. Otherwise, a runtime exception will be raised during the corresponding operation. as follows:

map[int]string                               //合法
map[string]struct{name, department string}   //合法
map[string]interface{}                       //合法
map[[]int]string                             //不合法
map[map[int]string]string                    //不合法

3.4.2 Value notation

Dictionary values ​​can be represented by compound literals.

A dictionary value of type map[string]bool:

map[string]bool{"Vim": true, "Emacs": true, "LiteIDE": true, "Notepad": false}

An empty dictionary value that does not contain any key-value pairs:

map[string]bool{}

3.4.3 Properties and basic operations

Like the pointer type and the slice type, the dictionary type is a reference type. Like slice values, a dictionary value will always hold a reference to the value of an underlying data structure.

Knowledge point : In the Go language, there is only "pass by value" but not "pass by reference". Whether the change of the parameter value inside the function will be reflected outside the function (or whether it is reflected in the original value of the parameter value) depends only on whether the type of the changed value is a value type or a reference type.

Because the dictionary type is a reference type, its zero value is nil . A dictionary type variable with a value of nil is similar to an empty dictionary with a length of 0. Reading to it will not cause any errors, but writing to it (adding or deleting key-value pairs) will cause a runtime panic. The value of an uninitialized dictionary type variable is nil .

Declare and initialize a dictionary type variable as follows:

editorSign := map[string]bool{"LiteIDE": true, "Notepad": false}

Add key-value pairs as follows:

editorSign["Vim"] = true

Find and get the element value corresponding to the specified key value in a dictionary through the index expression, as follows:

sign1 := editorSign["Vim"]

When there is no key-value pair with the key "Vim" in the value of editorSign, the variable sign1 will be assigned the zero value of the element type of editorSign, that is, false.

sign1, ok := editorSign["Vim"]

The variable ok will be of Boolean type, and its value indicates whether there is a key-value pair with the key "Vim" in the value of editorSign.
Delete key-value pairs as follows:

delete(editorSign, "Vim")

Note : Dictionary values ​​are not concurrency safe! The Go language officially believes that safe access control in multi-threaded scenarios is not required in most places where dictionary values ​​are used. To ensure concurrency safety, you need to use the structure type RWMutex in the standard library code package sync (a read-write mutex, often used for concurrency control in a multithreaded environment). This is temporarily explained in the follow-up blog post.

Guess you like

Origin blog.csdn.net/u012855229/article/details/115367479