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.
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)
}
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.
02Returned headers
A helper can change storage without changing the caller’s length
The receive helper appends a status code and keeps the new slice locally. The caller then reports its own visible length and explicitly exposes the reserved cells. Predict both observations.
package main
import "fmt"
func add(buf []int, code int) {
buf = append(buf, code)
fmt.Println("inside", len(buf))
}
func main() {
buf := make([]int, 1, 3)
buf[0] = 4
add(buf, 7)
fmt.Println("caller", len(buf), buf[:cap(buf)])
}
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.
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)
}
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.
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)
}
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.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?