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.
}

Analogy: Imagine a long train (backing array). A slice is a ticket that tells you:

Slice Internals


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>