Length vs Capacity of Slice in Go: Key Differences and Usage
length is the number of elements it currently holds, while its capacity is the total number of elements it can hold before needing to allocate more memory. The length changes as you add or remove elements, but the capacity only grows when the slice expands beyond its current limit.Quick Comparison
This table summarizes the key differences between length and capacity of a slice in Go.
| Aspect | Length | Capacity |
|---|---|---|
| Definition | Number of elements currently in the slice | Maximum number of elements slice can hold without reallocating |
| Type | int | int |
| Changes when | Elements are added or removed | Slice grows beyond current capacity |
| Access via | len(slice) | cap(slice) |
| Limits | Always ≤ capacity | ≥ length, can be larger |
| Memory | Reflects used elements | Reflects allocated space |
Key Differences
The length of a slice in Go tells you how many elements are currently stored and accessible. It changes whenever you add or remove elements using operations like append or slicing. For example, if you have a slice with 3 elements, its length is 3.
The capacity represents the total space allocated for the slice, which may be larger than the length. This extra space allows the slice to grow without needing to allocate new memory immediately. When you append elements beyond the current capacity, Go automatically allocates a bigger underlying array and copies the elements over, increasing the capacity.
In short, length is about how many elements you can use right now, while capacity is about how many elements the slice can hold before it needs to grow its storage.
Code Comparison
This Go code shows how length and capacity behave when appending elements to a slice.
package main import ( "fmt" ) func main() { slice := make([]int, 2, 4) // length 2, capacity 4 fmt.Printf("Initial slice: %v, length: %d, capacity: %d\n", slice, len(slice), cap(slice)) slice = append(slice, 10) fmt.Printf("After append 1: %v, length: %d, capacity: %d\n", slice, len(slice), cap(slice)) slice = append(slice, 20, 30) fmt.Printf("After append 2: %v, length: %d, capacity: %d\n", slice, len(slice), cap(slice)) }
Length vs Capacity Equivalent Explanation
This code snippet demonstrates how the length and capacity of a slice change as elements are added, highlighting the difference between the two.
package main import ( "fmt" ) func main() { // Create a slice with length 3 and capacity 5 s := make([]int, 3, 5) fmt.Printf("Initial: len=%d, cap=%d, slice=%v\n", len(s), cap(s), s) // Append elements within capacity s = append(s, 1, 2) fmt.Printf("After append within capacity: len=%d, cap=%d, slice=%v\n", len(s), cap(s), s) // Append element exceeding capacity s = append(s, 3) fmt.Printf("After append exceeding capacity: len=%d, cap=%d, slice=%v\n", len(s), cap(s), s) }
When to Use Which
Use length when you need to know how many elements are currently in the slice for iteration or processing. Use capacity when you want to optimize performance by preallocating enough space to avoid repeated memory allocations during appends. Knowing capacity helps you manage memory efficiently and avoid unexpected reallocations.
In practice, check length to control loops and logic, and check or set capacity when creating slices with make to improve speed when you expect to add many elements.