21. Slices
A window into an array!
Slice Type
Array size is fixed in Go. We can not add more elements to an existing array. This is limiting from a programmer's point of view. Go provides slices to address this concern.
Slices in Go can grow. Arrays are fixed length sequence where as slices are variable length sequence in Go. Slices are very lightweight data structures.A slice has three components: a pointer, a length, and a capacity.
Declaring slice
While declaring slice, we need to specify type of element slice will hold with empty pair of []
.
[]T
defines a slice of type T.
While declaring an array, we need to specify size while slice declaration has empty size. a
is an array below while s
is a slice.
Creating slice
Declaring slice using var does not allocate backing array for slice. Instead, we have to create slice using make
function. Make function accepts 3 parameters, first is type of slice, second is length of slice to create and optionally third parameter as capacity of slice.
s := make([]T,length,capacity)
If we know in advance, what capacity we need for slice, we can make slice with that capacity.
Accessing slice elements
Since slice is window into an array, its elements can be accessed by using square brackets []
with an index that begins at 0
similar to array access.
Zero values
When new slice is created using make
all its elements get initialized to its zero values.
Slice literals
If we know in advance what values a slice will start with, we can initialize the slice with those values using slice literal. We do not need to make
function call when using slice literals.
[]T{values}
Slice from an existing array
We can create slice from an existing array. To create slice from existing array, we need to specify start index and end index using syntax array[startIndex:endIndex]
If starting index is zero, it can be skipped. If end index equals length of array, it can be skipped.
Nil and empty slice
Go has both nil and empty slices. When we use var declaration, it initializes nil slice provided we do not use slice literal. If we use short variable declaration and create slice using make, it initializes empty slice. In case of nil slice, no backing array is created, while for empty slice backing array is present.
use len(s) == 0
, and not s == nil
for checking for empty slice.
Iterating over slice
Iteration over slice is similar to array iteration. Preferred way is to for...range
Type of slice and comparison
Unlike array, slice types are defined what type of data slice store. Comparison is not possible using ==
unlike arrays. For most use cases where comparison needs to be done, we need to write our own comparison code
Append
The advantage slices have over arrays is the fact that slices can increase capacity and hold more elements at run-time. To add elements to slice, we use built in append
function.
append
can be used to add one or more elements to slice. Append operation may create new slice if new elements to add do not fit in existing capacity of slice.
Whenever we do append operation, we reassigned the returned slice to original slice. Returned slice may or may not be new slice.
s := append(s, e)
GitHub Code
Blog Posts
Videos
Last updated