In golang, the slice length is the number of elements in the slice; the slice capacity is the number of elements in the underlying array starting from the index where the slice is created, that is, counting from the first element of the slice to its bottom layer The number of elements at the end of the array. The length and capacity of a slice can be calculated. The built-in method len() can obtain the length, and cap() can obtain the capacity; in the process of using the slice, if append() is used to make the length of the slice greater than the capacity of the slice, then the slice The capacity will be automatically expanded by doubling.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Go language slices have length and capacity.
The length of a slice is the number of elements in the slice
The capacity of a slice is in the underlying array starting from the index at which the slice was created The number of elements, counting from its first element to the end of its underlying array elements.
Slices are indexable, and the length can be obtained by the len() method. Slices provide a method for calculating capacity, cap(), which can measure how long the slice can be. When we use slicing, if we use append to make the length of the slice greater than the capacity of the slice, the capacity of the slice will be automatically expanded in double the form.
Slicing actually obtains a certain part of the array, len slice<=cap slice<=len array
The result of cap() determines the details of slice interception
var sTest01 []int func sliceTest01() { fmt.Printf("%T \n cap(sTest01) = %v \n", sTest01, cap(sTest01)) sTest01 = append(sTest01, 1, 2, 3, 4, 5, 6) fmt.Printf("%T \n cap(sTest01) = %v \n", sTest01, cap(sTest01)) }
Running result:
[]int cap(sTest01) = 0 []int cap(sTest01) = 6
It can be seen that at the beginning, the slice length is 0, and after adding elements, the actual length is 6
Examples that prove that arrays are value types and slices are reference types:
func sliceTest02() { x := [...]int{1, 2, 3, 4, 5, 6} y := []int{100, 200, 300, 400} w := x z := y w[0] = 777 z[0] = 999 fmt.Println("x = ", x, "\nw = ", w) fmt.Println("y = ", y, "\nz = ", z) }
Running results:
x = [1 2 3 4 5 6] w = [777 2 3 4 5 6] y = [999 200 300 400] z = [999 200 300 400]
As can be seen from the running results, the change of z affects the value of y , indicating that the slice is a reference type.
Slice does not have any data of its own, it is just a reference to the underlying array. Any modifications made to slice will be reflected in the underlying array. Arrays are value types, while slices are reference types
func sliceCap() { arr := [...]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"} fmt.Println("cap(arr) = ", cap(arr), arr) //截取数组,形成切片 s1 := arr[2:8] fmt.Printf("%T \n", s1) fmt.Println("cap(s1) = ", cap(s1), s1) //截取数组,形成切片 s2 := arr[4:7] fmt.Printf("%T \n", s2) fmt.Println("cap(s2) = ", cap(s2), s2) //截取数组,形成切片 s3 := s1[3:9] fmt.Printf("%T \n", s3) fmt.Println("cap(s3) = ", cap(s3), s3) //截取数组,形成切片 s4 := s2[4:7] fmt.Printf("%T \n", s4) fmt.Println("cap(s4) = ", cap(s4), s4) //证明切片是引用类型 s4[0] = "x" fmt.Println(arr, s1, s2, s3, s4) }
Running results:
cap(arr) = 11 [a b c d e f g h i j k] []string cap(s1) = 9 [c d e f g h] []string cap(s2) = 7 [e f g] []string cap(s3) = 6 [f g h i j k] []string cap(s4) = 3 [i j k] [a b c d e f g h x j k] [c d e f g h] [e f g] [f g h x j k] [x j k]
We can see from the results that interception of slices can only generate slices. If the content is not intercepted enough, the subsequent values will be given from the underlying array. If the length is not enough, an error will be reported.
To determine whether a slice is empty, it is inaccurate to use nil directly.
Golang allows slices where len is 0 but cap is not 0, or both are 0, so generally the length of the slice is obtained through len to determine whether it is an empty slice, instead of directly summing the slice and nil does a direct comparison.
【Related recommendations: Go video tutorial, Programming teaching】
The above is the detailed content of What is the length and capacity of golang slices. For more information, please follow other related articles on the PHP Chinese website!