Learning golang language (1): Hello World
Learning golang language (2): Variable
Learning golang language (3): Type-Boolean and numerical type
Learning golang language (4): Type-string
Learning golang language (5): Type-array
Learning golang language (6): Type-slice
In many application scenarios, arrays cannot meet our needs. When we initially define an array, we do not know the length required by the array. Therefore, we need an array (dynamic array) with dynamically varying sizes)
In the go language, this "dynamic array" becomes slice ).
But in fact, slice is not a dynamic array in the true sense, but a reference type. Slice always points to an underlying array. Slice statements can be the same as array statements. But it does not need to declare the length. The Slice length is variable.
Strictly speaking, slice has two attributes.Capacity(Capacity) andLength(Length), where capacity> = length.
Slice statement
You can create a slice as follows.
1. declare that a variable is a slice, and then use the built-in function make to initialize the slice.
Here, we first use the make function to define slice1. In this case, the capacity of slice1 is 5; the length is 5; then the make function is used to define slice2. In this case, the capacity of slice2 is 10, length = 5;
Therefore, make can be used to define slices in two ways,
Specify only the length. In this case, the slice length and capacity are the same;
Specify the length and capacity of the slice.
--------------------Note:--------------------
1. When the capacity is greater than the length, you still need to note that the maximum index is still Len (slice)-1. Otherwise, an error occurs when the index exceeds the boundary.
2. Use the: = symbol to declare a slice and initialize data at the same time, as shown below.
Slice: = [] Byte {'A', 'B', 'C', 'D', 'E '}
3. slices can be declared again from an array or an existing slice. The Slice uses array [I: J] to obtain the elements of the array index from I to J. Where I is the starting position and J is the location of the sleep. But does not contain array [J]. The length is J-I.
---------------------------------------------
In addition: When declaring an array, you must specify the length of the array in square brackets or use the (...) symbol to automatically calculate the length. When the slice is declared, there are no characters in square brackets.
Slice also has some simple operations
The default start position of a slice is 0. Slice [: N] is equivalent to slice [0: N].
The second sequence of slices is the length of the array by default. Slice [N:] is equivalent to slice [N: Len (slice)];
If the slice is obtained directly from an array, slice [:] can be like this, because the first sequence is 0 by default, and the second sequence is Len (slice), so slice [:] it is equivalent to slice [0: Len (slice)].
For example:
Output result:
Because slice is a reference type, when the reference changes the value of the element, all other references change the value. For example:
Output result
When we change slice1 [3] to 100, slice2 [0] also becomes 100.
---------------------------------------------
In terms of concept, the slice is like a struct and contains three elements:
A starting position pointing to the specified slice in the array;
Length, that is, the slice length, obtained through the built-in function Len;
The maximum length, that is, the maximum capacity of the slice, is obtained through the built-in function cap.
---------------------------------------------
Important features of Slice:Variable Length, You can learn through the following example.
Append Element
Output result
Here we initialize slice as a slice with capacity = 8; length = 4. Assign values to the first four elements and output the results. In addition, use the append function of go to append six elements to slice.
In this case, check the slice capacity = 16, length = 10, and slice elements. It is found that the length of the slice does change.
--------------------Note:--------------------
Append
If the length of the new slice is smaller than the capacity, the capacity will not change.
If the length of the new slice exceeds the capacity, go automatically allocates the capacity for the slice again. The capacity is twice the original size.
The above example shows that the capacity is from 8 => 16.
---------------------------------------------
This section describes how to use the append function to add elements to a slice. Now we will introduce a copy function to copy elements from one slice to another.
Copy Element
Output result
In the preceding example, we copy the slice1 element to slice2. Because slice2 has a length of 5, a maximum of five elements can be copied.
-------------------- Summary --------------------
When declaring an array, you must specify the length of the array in square brackets or use the (...) symbol to automatically calculate the length. When the slice is declared, there are no characters in square brackets.
Because the array length is fixed, the slice length is variable.
---------------------------------------------
Welcome to CAPTCHA! Learn the golang language together.