May 11, 2021 Go
Go language slices are abstractions of arrays.
The length of the Go array is imm changed, such a collection is not appropriate in a particular scene, Go provides a flexible, powerful built-in type slice ("dynamic array"), compared to the array, the length of the slice is not fixed, you can append elements, when appending may increase the capacity of the slice.
You can declare an array of unseeded sizes to define slices:
var identifier []type
The slice does not need to indicate 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 capacity, where capacity is an optional parameter.
make([]T, length, capacity)
Here len is the length of the array and the initial length of the slice.
s :=[] int {1,2,3 }
Direct initialization of the slice, which means that it is a slice type, and the initialization value of the slice is 1, 2, 3. its cap is len=3
s := arr[:]
Initialize slice s, which is a reference to the array arr
s := arr[startIndex:endIndex]
Create an element in arr from the undersecond startIndex to endIndex-1 as a new slice
s := arr[startIndex:]
The default endIndex is represented all the way to the last element of arr
s := arr[:endIndex]
The default starter Index will indicate that it starts with the first element of the arr
s1 := s[startIndex:endIndex]
Initialize slice s1 by slice s
s :=make([]int,len,cap)
Initialize the slice s with the built-in function make(), which identifies the slice as its element type int
Slices are indexable and can be obtained by the len() method.
Slices provide a way to calculate capacity cap() to measure how much a slice can reach up to.
Here are the 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 above instance runs the output as:
len=3 cap=5 slice=[0 0 0]
A slice defaults to nil, with a length of 0, before it is initialized, 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 above instance runs the output as:
len=0 cap=0 slice=[] 切片是空的
You can set the intercept slice by setting the lower and upper limits, 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 above code output results in:
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]
If we want to increase the capacity of the slices, we have to create a new, larger slice and copy all the original slices.
The following code describes the copy method from which the slice was copied and the append method that appended the new element 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 executes the output as follows:
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]