Home >Backend Development >Golang >What is the capacity and length of a slice in Go?
In Go, a slice is a flexible and powerful data structure that is built on top of an array. It has two key properties: length and capacity.
len()
function.cap()
function.Here is a simple example demonstrating the use of len()
and cap()
:
<code class="go">package main import "fmt" func main() { s := make([]int, 5, 10) // Creates a slice with length 5 and capacity 10 fmt.Printf("Length: %d, Capacity: %d\n", len(s), cap(s)) }</code>
In this example, the slice s
has a length of 5 and a capacity of 10.
The capacity of a slice in Go is immutable once it is created. You cannot directly modify the capacity of an existing slice. However, you can create a new slice with a different capacity using a few methods:
Using the make
function: You can create a new slice with a specified length and capacity.
<code class="go">newSlice := make([]int, len(oldSlice), newCapacity) copy(newSlice, oldSlice)</code>
Using the append
function: When you append elements to a slice, Go will automatically increase the capacity if necessary.
<code class="go">s := []int{1, 2, 3} s = append(s, 4, 5, 6) // This might increase the capacity if needed</code>
Using the copy
function: You can copy the contents of the old slice to a new slice with a different capacity.
<code class="go">newSlice := make([]int, len(oldSlice), newCapacity) copy(newSlice, oldSlice)</code>
Here’s an example to illustrate these methods:
<code class="go">package main import "fmt" func main() { s := []int{1, 2, 3} // Length: 3, Capacity: 3 fmt.Printf("Original: Length: %d, Capacity: %d\n", len(s), cap(s)) // Using make and copy newSlice := make([]int, len(s), 10) copy(newSlice, s) fmt.Printf("After make and copy: Length: %d, Capacity: %d\n", len(newSlice), cap(newSlice)) // Using append s = append(s, 4, 5, 6, 7) fmt.Printf("After append: Length: %d, Capacity: %d\n", len(s), cap(s)) }</code>
In Go, attempting to access an index beyond the length of a slice will result in a runtime panic. This is because the length of the slice defines the range of indices you can safely access.
Here’s an example:
<code class="go">package main import "fmt" func main() { s := []int{1, 2, 3} // Length: 3 fmt.Println(s[0]) // This is valid fmt.Println(s[3]) // This will cause a runtime panic }</code>
The error message you would see is something like this:
<code>panic: runtime error: index out of range [3] with length 3</code>
This panic occurs because index 3 is outside the bounds of the slice, whose length is 3.
The length and capacity of a slice in Go serve different purposes and have different roles:
len(s) - 1
, and attempting to access an index beyond this will result in a runtime panic.append
function without triggering memory reallocation.Here’s a summary of their differences:
Length:
len(s)
.Capacity:
cap(s)
.Here is an example illustrating these concepts:
<code class="go">package main import "fmt" func main() { s := make([]int, 3, 5) // Length: 3, Capacity: 5 fmt.Printf("Initial: Length: %d, Capacity: %d\n", len(s), cap(s)) s = append(s, 4) // Length: 4, Capacity: 5 fmt.Printf("After first append: Length: %d, Capacity: %d\n", len(s), cap(s)) s = append(s, 5) // Length: 5, Capacity: 5 fmt.Printf("After second append: Length: %d, Capacity: %d\n", len(s), cap(s)) s = append(s, 6) // Length: 6, Capacity might increase fmt.Printf("After third append: Length: %d, Capacity: %d\n", len(s), cap(s)) }</code>
In this example, the slice starts with a length of 3 and a capacity of 5. As we append elements, the length increases until it reaches the capacity. The capacity may increase when we exceed the original capacity.
The above is the detailed content of What is the capacity and length of a slice in Go?. For more information, please follow other related articles on the PHP Chinese website!