Go language slice (Slice)
Go language slices are an abstraction over arrays.
The length of Go arrays cannot be changed, so in specific scenarios such a collection is not very suitable. Go provides a flexible and powerful built-in type: slice ("dynamic array"). Compared with arrays, the length of a slice is not fixed; elements can be appended, and appending may increase the capacity of the slice.
Defining Slices
You can declare an array of unspecified size to define a slice:
var identifier []type
Slices do not need to specify a length.
Or usemake()Function to create a slice:
var slice1 []type = make([]type, len) 也可以简写为 slice1 := make([]type, len)
You can also specify the capacity, wherecapacityis an optional parameter.
make([]T, length, capacity)
Here len is the length of the array and also the initial length of the slice.
Slice Initialization
s :=[] int {1,2,3 }
Directly initialize the slice,[]Indicates that it is a slice type,{1,2,3}The initialization values are, in order:1,2,3, itscap=len=3。
s := arr[:]
Initialize a Slices, is a reference to the array arr.
s := arr[startIndex:endIndex]
Create a new slice from the elements of arr from index startIndex to endIndex-1.
s := arr[startIndex:]
When endIndex is defaulted, it means up to the last element of arr.
s := arr[:endIndex]
When startIndex is defaulted, it means starting from the first element of arr.
s1 := s[startIndex:endIndex]
Initialize slice s1 through slice s.
s :=make([]int,len,cap)
Through the built-in functionmake()Initialize a Slices,[]intDenotes a slice whose element type is int.
len() and cap() functions
Slices are indexable, and their length can be obtained by the len() method.
Slices provide the cap() method for calculating capacity, which can measure how long a slice can reach at most.
The following are specific examples:
Example
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 of the above example is:
len=3 cap=5 slice=[0 0 0]
Empty (nil) slice
Before a slice is initialized, it defaults to nil with a length of 0, for example:
Example
import "fmt"
func main() {
var numbers []int
printSlice(numbers)
if(numbers == nil){
fmt.Printf("The slice is empty")
}
}
func printSlice(x []int){
fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}
The output of the above example is:
len=0 cap=0 slice=[] 切片是空的
Slicing a Slice
You can set lower and upper bounds to slice a slice.[lower-bound:upper-bound], example as follows:
Example
import "fmt"
func main() {
/* Create a slice */
numbers := []int{0,1,2,3,4,5,6,7,8}
printSlice(numbers)
/* Print the original slice */
fmt.Println("numbers ==", numbers)
/* Print sub-slice from index 1 (inclusive) to index 4 (exclusive) */
fmt.Println("numbers[1:4] ==", numbers[1:4])
/* The default lower bound is 0 */
fmt.Println("numbers[:3] ==", numbers[:3])
/* The default upper bound is len(s) */
fmt.Println("numbers[4:] ==", numbers[4:])
numbers1 := make([]int,0,5)
printSlice(numbers1)
/* Print sub-slice from index 0 (inclusive) to index 2 (exclusive) */
number2 := numbers[:2]
printSlice(number2)
/* Print sub-slice from index 2 (inclusive) to index 5 (exclusive) */
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 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 you want to increase the capacity of a slice, you must create a new larger slice and copy the contents of the original slice into it.
The following code describes the copy method for copying slices and the append method for appending new elements to a slice.
Example
import "fmt"
func main() {
var numbers []int
printSlice(numbers)
/* Allows appending to an empty slice */
numbers = append(numbers, 0)
printSlice(numbers)
/* Add an element to the slice */
numbers = append(numbers, 1)
printSlice(numbers)
/* Add multiple elements at the same time */
numbers = append(numbers, 2,3,4)
printSlice(numbers)
/* Create slice numbers1 with twice the capacity of the previous slice */
numbers1 := make([]int, len(numbers), (cap(numbers))*2)
/* Copy the contents of numbers to 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 output of executing the above code is:
len=0 cap=0 slice=[] len=1 cap=1 slice=[0] len=2 cap=2 slice=[0 1] len=5 cap=6 slice=[0 1 2 3 4] len=5 cap=12 slice=[0 1 2 3 4]other extensions