A slice in Go is a dynamically-sized reference type that provides a view into a contiguous segment of an underlying array. Unlike arrays, which have a fixed size determined at compile time, slices act as references to an underlying array, managing a pointer, length, and capacity at runtime.Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Internal Structure
At runtime, a slice is represented as a struct with three fields. While historically represented byreflect.SliceHeader, this type was deprecated in Go 1.20 in favor of unsafe.Slice and unsafe.SliceData. Conceptually, the internal struct operates as follows:
- Pointer (
Data): A memory address pointing to the first element of the underlying array accessible through the slice. This is not necessarily the first element of the array itself. - Length (
Len): The number of elements currently accessible in the slice. - Capacity (
Cap): The total number of elements in the underlying array, counting from theDatapointer to the end of the array.
Declaration and Initialization
Slices can be initialized using slice literals, themake built-in function, or by slicing an existing array or slice.
Slicing Operations
You can create a slice from an existing array or slice using the half-open range syntax[low:high]. Go also supports a full slice expression [low:high:max] to explicitly control the resulting slice’s capacity.
Memory Sharing and Mutation
Because a slice contains a pointer to an underlying array, mutating the elements of a slice directly mutates the underlying array. Any other slices sharing that same underlying array will reflect those changes.Data pointer still references the original array, the garbage collector cannot reclaim the unused memory, which can lead to severe memory leaks.
Copying Slices (copy)
To safely duplicate data and decouple a slice from a shared underlying array (preventing both unintended mutations and memory leaks), use the built-in copy function. copy transfers elements from a source slice to a destination slice, returning the number of elements copied. The destination slice must be pre-allocated with sufficient length.
Growth Mechanics (append)
The append built-in function adds elements to the end of a slice. If the slice’s underlying array has sufficient capacity to accommodate the new elements (Len + len(newElements) <= Cap), append places the new elements in the existing array and returns a new slice header with an incremented Len.
If the required length exceeds the current capacity, Go triggers an automatic reallocation:
- A new, larger underlying array is allocated in memory.
- Existing elements are copied from the old array to the new array.
- The new elements are appended.
- A new slice header pointing to the new array is returned.
Function Arguments (Pass-by-Value Semantics)
While slices are classified as reference types, Go passes all arguments by value. When a slice is passed to a function, Go passes a copy of the slice header struct (containing theData pointer, Len, and Cap).
Because the copied slice header points to the same underlying array, modifying the elements at specific indices inside the function will mutate the caller’s data. However, modifying the slice’s length or capacity (e.g., via append) only updates the function’s local copy of the slice header. To reflect structural changes in the caller’s scope, the function must return the modified slice and the caller must reassign it.
Master Go with Deep Grasping Methodology!Learn More





