First of all, you need to know that in Golang, the underlying layer of the slice is actually an array.
1. Length of slice:
The length of a slice refers to the number of elements in the slice. You can use the len() function to query the length of its slice.
Example 1:
package main import "fmt" func main(){ slice := []int{1,3,2,6,4} # Call len to view length length := len(slice) ("The length of slice01 is: %v", length) } // Output result// The length of slice is: 5
In the above code, the variable length is the length of the slice (the result is 5)
2. Slice capacity:
The capacity of a slice refers to the length of the underlying array of the slice. You can use the cap() function to view the capacity.
Example 2:
package main import "fmt" func main(){ slice := []int{1,3,2,6,4} // Call cap to view capacity capacity := cap(slice) ("Slice01's capacity is: %v", capacity) } // Output result// The capacity of slice is: 5
In the above code, the variable capacity is the capacity of the slice (the result is also 5)
3. The difference cannot be seen through the above two examples. Let’s look at the following example:
Example 3
package main import "fmt" func main(){ // Create slice through the make function, 4 indicates the length of the slice, 10 indicates the capacity of the slice slice := make([]int, 4, 6) length := len(slice) capacity := cap(slice) ("The length of slice01 is:%v\n", length) ("Slice01's capacity is:%v\n", capacity) } // Output result// The length of slice01 is: 4// The capacity of slice01 is: 6
You can imagine it as: when executingslice := make([]int, 4, 10)
At the bottom, the program creates an array of length 6 at the bottom. The slice slice references the first 4 elements, and generates the slice slice. The current slice value is: {0, 0, 0, 0}, and the remaining 2 elements are not referenced for the time being.
4. View the changes in slices and underlying arrays by adding slices
We know that the main difference between slices and arrays in golang is that the array is fixed-length and the slices are variable-length. But we areExample 3The capacity of the slice is 6. So what will happen if there are more than 6 elements?
Example 4
package main import "fmt" func main(){ // Create slice through the make function, 4 indicates the length of the slice, 10 indicates the capacity of the slice slice := make([]int, 4, 6) length := len(slice) capacity := cap(slice) ("The length of slice01 is:%v\n", length) ("Slice01's capacity is:%v\n", capacity) //Add elements into slice slice for the first time slice = append(slice, 1) //Print the length, capacity, memory address of slice (memory address of slice[0]) ("\n\nFirst result:") ("The length of slice is: %v\n", len(slice)) ("Slice's capacity is:%v\n", cap(slice)) ("Slice's memory address is: %v\n", &slice[0]) // Add elements to slice for the second time slice = append(slice, 1) //Print the length, capacity, memory address of slice (memory address of slice[0]) ("\n\nSecond result:") ("The length of slice is: %v\n", len(slice)) ("Slice's capacity is:%v\n", cap(slice)) ("Slice's memory address is: %v\n", &slice[0]) // Add elements to slice for the third time slice = append(slice, 1) //Print the length, capacity, memory address of slice (memory address of slice[0]) ("\n\nThird result:") ("The length of slice is: %v\n", len(slice)) ("Slice's capacity is:%v\n", cap(slice)) ("Slice's memory address is: %v\n", &slice[0]) } // The length of slice01 is: 4// The capacity of slice01 is: 6 // The first result: the length of slice is: 5// The capacity of slice is: 6// The memory address of slice is: 0xc00000e390 // The second result: the length of slice is: 6// The capacity of slice is: 6// The memory address of slice is: 0xc00000e390 // The third result: the length of slice is: 7// The capacity of slice is: 12// The memory address of slice is: 0xc000060060
passExample 4We can find that when we initialize the slice length to 4 and the capacity to 6.
When the first and second elements are added to the slice, only the length of the slice increases, the capacity and memory address do not change
When the third time the element is added to the slice, the length is increased by 1, the capacity becomes twice as high as before, and the memory address also changes.
From this we can infer:
**Summary: **When the length of the slice element exceeds the default capacity, the program will recreate an underlying array and slice binding, and the length of the new array is twice the original (the capacity of the slice becomes twice the original)
5. Pay attention to usage:
Example 5
package main import "fmt" func main() { slice := make([]int, 4, 4) slice2 := make([]int, 4, 5) // Call the test function defined below test(slice) test(slice2) // Output the result after calling the test function from two slices ("Slice result: %v\n", slice) ("Slice2's result: %v\n", slice2) } func test(s []int) { s = append(s, 1) for i := 0; i < len(s); i++ { s[i] = 100 } } // The result of slice: [0 0 0 0]// The result of slice2: [100 100 100 100]
Different slice capacity leads to different final results.
Summarize
This is the end of this article about the difference between slice length and capacity in Golang. For more relevant content on slice length and capacity, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!