Golang - Data Structures - Slices In-Depth
Publish date: 2025-09-06
Tags:
<a href="https://programmercave.com/tags/Go/">Go</a>
Introduction
To truly master slices, you must understand their internal structure. A slice is just a small struct, called a slice header, that describes a section of a larger, hidden array called the backing array.
The Slice Header
Every slice in Go can be thought of as this struct:
type SliceHeader struct {
Pointer // A pointer to the first element of the backing array that the slice can access.
Length // The number of elements the slice contains.
Capacity // The number of elements in the backing array from the start of the slice to the end of the array.
}
- Pointer: Points to where the slice’s view of the data begins.
- Length (
len()
): The number of elements currently in the slice. This is how many elements you can access with an index, froms[0]
tos[len(s)-1]
. - Capacity (
cap()
): The maximum number of elements the slice can hold without reallocating its backing array. It’s a measure of how much the slice can grow.
Analogy: Imagine a long train (backing array). A slice is a ticket that tells you:
- Which car your ticket starts at (Pointer).
- How many cars your ticket is valid for (Length).
- How many cars are left until the end of the train from where you started (Capacity).
Visualizing Length and Capacity
Let’s create a slice from an array.
package main
import "fmt"
func main() {
// The backing array: has 7 elements
backingArray := [7]string{"A", "B", "C", "D", "E", "F", "G"}
// Create a slice from index 2 up to (but not including) index 5
mySlice := backingArray[2:5]
fmt.Printf("Slice: %v\n", mySlice)
fmt.Printf("Length: %d\n", len(mySlice))
fmt.Printf("Capacity: %d\n", cap(mySlice))
}
Output:
Slice: [C D E]
Length: 3
Capacity: 5
Here’s what this looks like in memory:
Backing Array: [ A | B | C | D | E | F | G ]
^ ^ ^ ^ ^ ^ ^
Index: 0 1 2 3 4 5 6
mySlice Header:
- Pointer: Points to index 2 ("C")
- Length: 3 (Elements "C", "D", "E")
- Capacity: 5 (Elements "C", "D", "E", "F", "G" are available from the pointer's position)
Slices are a fundamental data structure in Go, providing a flexible way to work with sequences of elements. Understanding their length, capacity, and backing arrays is crucial for efficient memory management and predictable behavior. This document delves into these concepts with clear explanations and practical examples.
Tags:
<a href="https://programmercave.com/tags/Go/">Go</a>