← Fundamentals
Fundamentals

Go slices, length, and capacity

Follow every header and every future read

Follow a Go slice’s descriptor and backing cells through growth and reuse.

Outcome

Retain returned slice headers, bound append sharing, and preserve aliased inputs during buffer reuse.

Terminology (3)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Slice header
The slice value describing backing storage, length and capacity; copying it retains the storage relationship.
Capacity boundary
The extent available from a slice’s start before append needs a new backing array.
Overlapping input
A source range sharing cells with a destination that an operation writes.

The slice header and its backing array

A Go slice value describes a segment of a backing array through its starting location, length and capacity. Length bounds ordinary indexing; capacity describes how far the slice can extend from its start. Several slice values can describe overlapping parts of one array.

Keep two questions separate during an edit: which descriptor is updated, and which backing cells are written? Passing a slice copies its descriptor. Growth, reslicing and buffer reuse then need to be traced against every retained slice that may read those cells later.

01Backing storage

A slice has its own extent

A Go ingestion worker collects integer status codes in a reusable buffer. A diagnostic preview extends a selected batch while the received codes remain available. Before the next processing pass, the worker also compacts unread codes and joins a queued chunk that may be a slice of the same receive storage.

The worker first selects two codes from a received batch. Before extending or recycling that selection, check its visible length, remaining capacity and the destination of a local element write.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

func main() {
	backing := []int{10, 20, 30, 40}
	window := backing[1:3]
	window[0] = 9

	fmt.Println(len(window), cap(window), backing)
}
Which output follows from this program?
Go · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
go run main.go

Save one complete example at a time in a fresh folder under the selected filename. Run the shown checking/build command before execution. The browser lab supplies its editor, stdin fixtures and native runner.

03Capacity boundary

A third index changes the capacity limit

The preview selects the same two codes but also supplies a maximum index. This probe reports its extent, then writes one existing element without appending. Predict what the owner reports.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

func main() {
	owner := []int{10, 20, 30, 40}
	window := owner[1:3:3]
	fmt.Println("window", len(window), cap(window))

	window[0] = 9
	fmt.Println("owner", owner)
}
Which length, capacity and owner report follow?

04Overlapping inputs

One safe operation can overwrite another input

The worker has retained future as a queued chunk. It then moves unread codes to the buffer’s front with one overlapping append. Predict what both retained slices report after that write.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

func main() {
	buf := []int{10, 20, 30, 40, 50}
	future := buf[:2]
	kept := append(buf[:0], buf[2:]...)

	fmt.Println("kept", kept)
	fmt.Println("future", future)
}
Which output follows from this program?
Loading A safe append can overwrite another retained input…

05Practice

Repair the reusable code buffer

Repair the worker’s reusable status-code buffer. Keep an appended code visible to its caller, extend an editable diagnostic preview without changing the received batch, then prepare unread codes plus a queued chunk for the next pass. Each report starts with a fresh buffer from the same seed.

Input is seed count, capacity, unread start/end, chunk start/end, appended code, then seed values. Valid supplied bounds fit the seed and capacity is at least its length. The preview source remains unchanged even after the caller edits the result. Both portions of the joined batch use their original function-entry values, including when their slices share storage.

Reuse the input’s starting storage when the complete joined length fits capacity; otherwise use new storage. Temporary snapshots are allowed, an empty result needs no allocation, and no exact new capacity is assessed.

Start the lab

Keep the grown extent, protect the editable preview, then preserve queued inputs while recycling the batch buffer.

  1. easy · F-GOSLICE-V4-1 Retain the grown buffer
  2. medium · F-GOSLICE-V4-2 Protect an extended window
  3. hard · F-GOSLICE-V4-3 Preserve inputs during reuse
Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.