This is a creation in Article, where the information may have evolved or changed.
First, the concept of slice:
Slices (slice) Slice is the concept that the previous structure contains three fields: the pointer, length, and capacity of an array. The slice supports the [] operator to access the elements of the underlying array. The length of the slice returned by the built-in Len function. The built-in Cap function returns the capacity of the slice.
A slice is a reference type, which means that if you assign a slice to another slice, the two slices will reference the same underlying array.
Second, how to create slice
The following two methods are available for creating tiles:
(1) Create directly:
S: = make ([]int, 10)
An integer type slice named S, with 10 elements (length equal to 10) is created
If you do not assign a value to slice after creation, the value of slice is the default value for creating the type, such as:
Packagemain
Import "FMT"
Funcmain () {
S:=make ([]int,10)
Fmt. PRINTLN (s)
}
Output
[0000000000]
Or
s: = []int{1,2,3,4,5}
An integer type slice named S with 5 elements is created, and each element is assigned a value of 1,2,3,4,5
(2) Indirect establishment:
First build array A and then create slices with arrays s
var a [10]int
S: = A[1:5]//a[1],a[2],a[3],a[4]
Created an integer type slice named s length of 4 capacity of 10, that is Len (s) equals 4,cap (s) equals 10 (the effect is similar to using make ([]int,4, 10))
Slice s point to array a[1],a[2],a[3],a[4]
When the value of array a changes, the value of the slice s also changes, because the slice s refers to the array a
(If multiple slices point to the same array, the values of the multiple slices that point to it change when the underlying array value changes)
You can also re-specify:
s = a[0:8]//s point to array a[0] to a[7]
s = s[2:5]//s points to the s[2],s[3],s[4 of the original slice] three elements.
Always point to X to y-1 elements when using [x:y]
When x is the first element or y is the last element, you can omit not to write
Package Main
Import "FMT"
Func Main () {
Vara = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
s:= A[1:5]//Represents the element value in the A array ah a[1] to a[4]
Fmt. Println (s)//output: [2 3 4 5]
a[2]= 21
Fmt. Println (s)//output:[2 21 4 5], indicating that the slice s and array a point to the same memory space
}
Output
[2 3 4 5]
[2 21 4 5]
add element:
Use the Append function to increase the length of the slice
s: = []int{1,2,3}//Build section S
s = Append (S, 4, 5)//When appending is a value (can be 1 or more)
It adds two elements to the slice s and assigns a value of 4 and 5
You can also append slices to slices
A: = []int{6, 7}//Build Slice A
s = Append (S,a ...) When you append a slice, there are three points behind it.
Append slice A to slice s to obtain S equals [1,2,3,4,5,6,7]
s = Append (S[2:5],a ...) To obtain S equals [3,4,5,6,7]
When the number of appended elements is greater than the capacity of this slice, the function append is
The original slice s re-allocates space, so s does not reference the original array or the appended array
If not ... will report an error and not be able to work; This is the difference between the append value and the Append slice
Package Main
Import "FMT"
Func Main () {
s:= []int{1, 2, 3}//Building a slice s
s= Append (S, 4, 5)//When appending is a value (can be 1 or more)
Fmt. PRINTLN (s)
a:= []int{6, 7}//Building a slice
s= Append (S, a ...)//When a slice is appended, there are three points behind it
Fmt. PRINTLN (s)
s= append (S[2:5], a ...)//Draw S equals [3,4,5,6,7]
Fmt. PRINTLN (s)
}
Output
[1 2 3 4 5]
[1 2 3 4 5 6 7]
[3 4 5 6 7]
and a function copy.
The copy function is used to copy data from one array or slice to another slice
N1: = Copy (S, A[x:y])
Copy array or slice A into s and reverse the number of elements copied to N1
A can be an array or a slice, but s can only be slices
The value of the array element is copied instead of the reference
The number of elements copied is Len (A[x:y]) and the lesser of Len (s)
Cases:
var a = [...] Int{0, 1, 2, 3, 4, 5, 6, 7}
var s = make ([]int, 6)
N1: = Copy (S, a[:])//Description: Copy all values of a to S
N2: = Copy (S, s[2:])//Description: Copy the value of the specified range to the start of S
N3: = Copy (S[4:6], a[6:8])//Description: Copy the value of the specified range to the specified position of s
Package Main
Import "FMT"
Func Main () {
Vara = [...] Int{0, 1, 2, 3, 4, 5, 6, 7}
VARs = Make ([]int, 6)
n1:= copy (S, a[:])//Description: Copy all values of a to S
Fmt. PRINTLN (n1)//Number of assignments
Fmt. PRINTLN (s)//Result after assignment
n2:= copy (S, s[2:])//Description: Copy the value of the specified range to the start of S
Fmt. Println (N2)
Fmt. PRINTLN (s)
n3:= copy (S[4:6], a[6:8])//Description: Copy the value of the specified range to the specified position of s
Fmt. Println (N3)
Fmt. PRINTLN (s)
}
Output
6
[0 1 2 3 4 5]
4
[2 3 4 5 4 5]
2
[2 3 4 5 6 7]
Third, note:
Slice is a pointer rather than a value.
The pointer ratio is much smaller, so we'll pass the slice as a function parameter.
Array is more performance-pass as a function parameter.
Slice is a pointer to an array mechanism that has two basic functions
Len and Cap.
Look at the following illustration:
Slice is a pointer to an array with a point, Len (the number of actual values in the array), CAP (the capacity of the array)
For example, above this slice, it points to the array is [3]int, where the first two have values, the third is empty
So
Len (slic) = 2
Cap (SLIC) = 3
The APPEND function is understood to add a value to the slice, if it does not reach the capacity (LEN<CAP) then add the value directly to the array, if it reaches the capacity (len = cap) then add a new element space, put the value inside