0
0
GoComparisonBeginner · 3 min read

Length vs Capacity of Slice in Go: Key Differences and Usage

In Go, a slice's 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.

AspectLengthCapacity
DefinitionNumber of elements currently in the sliceMaximum number of elements slice can hold without reallocating
Typeintint
Changes whenElements are added or removedSlice grows beyond current capacity
Access vialen(slice)cap(slice)
LimitsAlways ≤ capacity≥ length, can be larger
MemoryReflects used elementsReflects 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.

go
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))
}
Output
Initial slice: [0 0], length: 2, capacity: 4 After append 1: [0 0 10], length: 3, capacity: 4 After append 2: [0 0 10 20 30], length: 5, capacity: 8
⚖️

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.

go
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)
}
Output
Initial: len=3, cap=5, slice=[0 0 0] After append within capacity: len=5, cap=5, slice=[0 0 0 1 2] After append exceeding capacity: len=6, cap=10, slice=[0 0 0 1 2 3]
🎯

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.

Key Takeaways

Length is the current number of elements in a slice; capacity is the total allocated space.
Length changes with adding/removing elements; capacity only grows when needed.
Use length to control loops and access elements safely.
Use capacity to preallocate memory and optimize append operations.
Appending beyond capacity causes Go to allocate a bigger underlying array.