This is a creation in Article, where the information may have evolved or changed.
Go language Programming: Collection types
Go Collection
This chapter mainly describes the following types:
The value, the pointer, and the type of the reference. In addition, there are some built-in types for the Go language: arrays, slices, and mappings.
Typically, the variable holds the corresponding value. Values are copied once when passed to a function or method, which is very inexpensive for Boolean types or numeric types. Passing a string by value is also inexpensive because the string is immutable. However, if you modify an incoming string, the cost can be significant.
In the go language, arrays are passed by value. Fortunately, we do not use arrays, but rather slices.
For pointers, usage is similar to C. Both the & address operator and the * dereference operator. Its basic usage is similar to the C pointer. When generating an instance of a struct, we can use new (Type) to generate a pointer to this instance, or a &type{} method to generate a pointer. The latter can also be initialized.
In addition to the two, there are reference types. The delivery of reference types is also inexpensive and syntax is the same as values.
Both the map and the slice are references, so any modifications we have to them are visible.
In addition to mapping and slicing, channels, functions, and methods are reference types. The reference has no special syntax, it is the same as the use of the value.
Arrays and slices
The go language array is fixed-length and has the same type. The syntax is created as follows:
[length]type[N]type{value1, ...., valueN}[...]type{value1, ...,valueN}
In three cases, it is important to note that the length of the array is deterministic and needs to be determined in []. Either you start with the certainty, or you can infer it from the array contents. The length of the array can be obtained with the Len function.
In general, we can use arrays to get slices. If you create a slice, use the following method:
make([]type, length, capacity)make([]type, length)[]type{}[]type{value1,..., valueN}
The length of the slice is variable when [] does not need to be determined. Built-in function make can be used to create slices, mappings, and channels. In other words, make can be used to create most of the reference types. If you don't use make, you can do it with {}.
Index, split slice: Same as python.
Traversing slices:
Divided into for i: = Range slice and for _, Value: = Range Slice
The first value is an index.
Modify slices: You can use the Append function to add new slices, which copy can be used to replicate.
Sorting and searching slices: multiple functions
Mapping
Mapping in the Go language is similar to dict in Python, which is used to hold key-value pairs.
Mappings are created in a similar way to slices, and can be created by making:
make(map[keytype]valuetype, initialcap)make(map[keytype]valuetype)map[keytype]valuetype{}map[keytype]valuetype{key1:value1, key2:value2}
: either created by make or by {}
Other methods of use are similar to Python.