In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.