How to understand slicing of Go language
This article mainly explains "how to understand Go language slices". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it to study and learn "how to understand Go language slices" together!
directory
I. Definition of slices
Declare an array of unspecified size to define slices
Use the make() function to create slices
Second, the slice is indexable
len() and cap() functions
III. Slice interception
IV. Increase the capacity of slices
Description:
Go slices are abstractions of arrays.
Go array length can not be changed, in certain scenarios such a collection is not suitable, Go provides a flexible, powerful built-in type slice ("dynamic array"), compared with the array slice length is not fixed, you can add elements, in addition to the slice may increase the capacity.
I. Definition of slices
Note: Slice does not need to specify length
1. Declare an array of unspecified size to define slices var identifier []type//e.g. var slice []int2. Use make() function to create slices var slice1 []type = make([]type, len)//also can be abbreviated as slice1 := make([]type, len)//e.g. slice := make([]type, len) 2. Slice is indexable 1, len() and cap() functions
Length can be obtained by len() method.
Slice provides a way to calculate capacity cap() measures how long slices can be up to
package mainimport "fmt"func main(){ var number = make([]int,3,5) fmt.Printf("len=%d cap=%d slice=%v\n",len(number),cap(number),number)}//len=3 cap=5 slice=[0 0 0]
2. Nil slice
A slice defaults to nil before it is initialized and has a length of 0
package mainimport "fmt"func main(){ var number []int fmt.Printf("len=%d cap=%d slice=%v\n",len(number),cap(number),number)}//len=0 cap=0 slice=[]
You can set the intercept slice by setting lower and upper limits [lower-bound:upper-bound]
package mainimport "fmt"func main() { //Create slice number := []int{0, 1, 2, 3, 4, 5, 6, 7, 8} printSlice(number) //Print original slides fmt.Println("number == ", number) //Print subslices from index 1 to index 4 fmt.Println("number == ", number[1:4]) //Print sub-slice default lower limit fmt.Println("number == ", number[:3]) //Print sub-slice default upper limit fmt.Println("number == ", number[4:])}func printSlice(x []int) { fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)}
Results:
IV. Increase the capacity of slices
Principle: Create a new larger slice and copy the contents of the original slice
append() and copy() functions package mainimport ( "fmt")func main() { //Create slice var number []int printSlice(number) //Allow blank slices to be added number = append(number,0) printSlice(number) //add an element to the slice number = append(number,1) printSlice(number) //add multiple elements simultaneously number = append(number,2,3,4) printSlice(number) //creates a new slice twice the size of the previous slice number1 := make([]int,len(number),(cap(number))*2) //copy the contents of number into number1 copy(number1,number) printSlice(number1)}func printSlice(x []int) { fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)} Thank you for reading, the above is the content of "How to understand Go language slices", after learning this article, I believe you have a deeper understanding of how to understand Go language slices, the specific use of the situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!