Go language slice (Slice)
Go language slice is an abstraction of arrays.
The length of Go arrays cannot be changed. In certain scenarios, such collections are not suitable. Go provides a flexible and powerful built-in type slice ("dynamic array"). Compared with arrays The length of the slice is not fixed. Elements can be appended, which may increase the capacity of the slice.
Define a slice
You can declare an array of unspecified size to define a slice:
var identifier []type
The slice does not need to specify the length.
Or use the make() function to create a slice:
var slice1 []type = make([]type, len) 也可以简写为 slice1 := make([]type, len)
You can also specify the capacity, where capacity is an optional parameter.
make([]T, length, capacity)
Here len is the length of the array and is also the initial length of the slice.
Slice initialization
s :=[] int {1,2,3 }
Directly initialize the slice, [] indicates the slice type, {1,2,3} initialization value is 1,2,3 in order. Its cap=len=3
s := arr[:]
Initialize slice s, which is a reference to array arr
s := arr[startIndex:endIndex]
Create the elements in arr from subscript startIndex to endIndex-1 as a new slice
s := arr[startIndex:]
Missing When endIndex is omitted, it will represent up to the last element of arr
s := arr[:endIndex]
The default startIndex will represent starting from the first element of arr
s1 := s[startIndex:endIndex]
Initialize slice s1 through slice s
s :=make([]int,len,cap)
Initialize slice s through the built-in function make(), []int identifies the slice whose element type is int
len() and cap() functions
The slice is Indexable, and the length can be obtained by the len() method.
Slices provide a method for calculating capacity. cap() can measure how long a slice can be.
The following are specific examples:
package main import "fmt" func main() { var numbers = make([]int,3,5) printSlice(numbers) } func printSlice(x []int){ fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x) }
The output result of the above example is:
len=3 cap=5 slice=[0 0 0]
Empty (nil) slice
A slice is in the future The default is nil before initialization, and the length is 0. The example is as follows:
package main import "fmt" func main() { var numbers []int printSlice(numbers) if(numbers == nil){ fmt.Printf("切片是空的") } } func printSlice(x []int){ fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x) }
The output result of the above example is:
len=0 cap=0 slice=[] 切片是空的
Slice interception
You can set the lower limit and The upper limit is used to set the interception slice [lower-bound:upper-bound]. The example is as follows:
package main import "fmt" func main() { /* 创建切片 */ numbers := []int{0,1,2,3,4,5,6,7,8} printSlice(numbers) /* 打印原始切片 */ fmt.Println("numbers ==", numbers) /* 打印子切片从索引1(包含) 到索引4(不包含)*/ fmt.Println("numbers[1:4] ==", numbers[1:4]) /* 默认下限为 0*/ fmt.Println("numbers[:3] ==", numbers[:3]) /* 默认上限为 len(s)*/ fmt.Println("numbers[4:] ==", numbers[4:]) numbers1 := make([]int,0,5) printSlice(numbers1) /* 打印子切片从索引 0(包含) 到索引 2(不包含) */ number2 := numbers[:2] printSlice(number2) /* 打印子切片从索引 2(包含) 到索引 5(不包含) */ number3 := numbers[2:5] printSlice(number3) } func printSlice(x []int){ fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x) }
The output result of executing the above code is:
len=9 cap=9 slice=[0 1 2 3 4 5 6 7 8] numbers == [0 1 2 3 4 5 6 7 8] numbers[1:4] == [1 2 3] numbers[:3] == [0 1 2] numbers[4:] == [4 5 6 7 8] len=0 cap=5 slice=[] len=2 cap=9 slice=[0 1] len=3 cap=7 slice=[2 3 4]
append( ) and copy() functions
If we want to increase the capacity of the slice, we must create a new larger slice and copy the contents of the original slice.
The following code describes the copy method for copying a slice and the append method for appending new elements to the slice.
package main import "fmt" func main() { var numbers []int printSlice(numbers) /* 允许追加空切片 */ numbers = append(numbers, 0) printSlice(numbers) /* 向切片添加一个元素 */ numbers = append(numbers, 1) printSlice(numbers) /* 同时添加多个元素 */ numbers = append(numbers, 2,3,4) printSlice(numbers) /* 创建切片 numbers1 是之前切片的两倍容量*/ numbers1 := make([]int, len(numbers), (cap(numbers))*2) /* 拷贝 numbers 的内容到 numbers1 */ copy(numbers1,numbers) printSlice(numbers1) } func printSlice(x []int){ fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x) }
The above code execution output result is:
len=0 cap=0 slice=[] len=1 cap=2 slice=[0] len=2 cap=2 slice=[0 1] len=5 cap=8 slice=[0 1 2 3 4] len=5 cap=16 slice=[0 1 2 3 4]