Go Slices
External
Internal
Overview
A slice is a descriptor, or a header that defines a contiguous segment of an underlying array, stored separately from the slice variable. A slice is not an array, a slice describes a piece of an array, and it is the idiomatic Go way of manipulating sequential, indexed data. Given that declaring a slice will create the backing array if no array is explicitly provided, you can almost always use a slice instead of an array. The slice provides access to a numbered sequence of elements from that array. A slice type denotes the set of all slices of arrays of its element type.
A slice contains a pointer to the underlying array, a length and a capacity. More details are available in the Structure section. A slice, once initialized, is always associated with the underlying array that holds its elements. The slice shares storage with its array, and other slices of the same array. A distinct slice that shares the underlying array with an original slice can be created by slicing the original slice with a slice expression.
Once the slice is initialized and the association with the underlying array is established, the association never changes, and the underlying array never changes, it cannot be grown or shrunk. However, a slice can be grown beyond the limits imposed by the original array, by allocating a new array under the covers, creating a new slice, copying elements across and using the second slice as the first slice. For more details, see append()
below.
Slices are passed by value, like an other Go variable, but when that happens, the slice argument of a function and the internal variable copy of the slice still share the underlying array.
The Go documentation used to refer to slices as reference types, but not anymore. The "reference type" terminology was removed from Go documentation. The slices are sometimes referred to as dynamic arrays.
Structure
Under the covers, a slice is a data structure that contains a pointer, the length of the slice and the capacity of the slice. These elements are referred to as contents, fields or components of the slice. You can think of it as being built like this:
type sliceHeader struct {
fistElement *T // pointer to the underlying array element that is the first element of the slice
length int
capacity int
}
Pointer
The pointer field contains a pointer to the underlying array element that represents the first element of the slice. The slice indexing is zero-based, so technically this would be slice element with the index 0.
Length
A slice's length represents the number of elements in the slice. "Length" and "size" can be used interchangeably. Unlike for arrays, the slice length can change. Describe how the length of a slice can change. Is there any other way than append()?
If an element beyond the length of the slice is accessed for read or write with the index operator, the Go runtime panics, even if the slice has sufficient capacity:
panic: runtime error: index out of range [4] with length 3
The same is true if an element is accessed with a negative array.
The length of a slice is reported by the len()
function.
Capacity
Slices and Pass-by-Value
Go uses pass-by-value, so when a slice argument is passed to a function, the internal fields are copied across on the function stack, including the pointer to the underlying array. Therefore, the underlying array data structure is intrinsically shared, even in case of pass-by-value. Both the original slice header and the copy of the header passed to the function describe the same array. If the function changes the underlying array via its copy of the slice, the modified elements can be seen outside the function through the original slice variable.
However, if the function modifies the elements of the slice, like the length, or the pointer to the array element, those changes naturally do not propagate outside of the function. Only the changes to the underlying array do.
nil and Empty Slice
Declaration and Initialization
Initialization with make()
Initialization with a Composite Literal
Initialization by Slicing the Underlying Array
Given an array variable, a slice of it can be declared by using the slice expression.
var aa [100]int // zero-value array declaration
var ss = aa[10:20] // slice declaration with slice expression
Operators
Indexing Operator []
Negative indexes are not supported, attempting to access a slice with a negative index generates a runtime panic.
Slice Expressions
Slice Functions
len()
Reports the slice length.
cap()
copy()
append()
make()
See Initialization with make() above.
new()
The slices Package
TODEPLETE
Deplete https://go.dev/wiki/SliceTricks into this page.