The slice type is an abstraction built on top of Go’s array type
Slices Are Like References to Arrays
A slice does not store any data (except the slice header, where the len
, cap
and ZerothElement
is stored), it just describes a section of an underlying array.
Changing the elements of a slice modifies the corresponding elements of its underlying array.
Other slices that share the same underlying array will see those changes.
len()
And cap()
A slice has both a length and a capacity.
The length of a slice is the number of elements it contains.
The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice.
The length and capacity of a slice s
can be obtained using the expressions len(s)
and cap(s)
.
You can extend a slice’s length by re-slicing it, provided it has sufficient capacity.
Creating a Slice with Make
Slices can be created with the built-in make
function; this is how you create dynamically-sized arrays.
The make
function allocates a zeroed array and returns a slice that refers to that array:
To specify a capacity, pass a third argument to make
: