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.
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.
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.
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.
When new slice is created using make all its elements get initialized to its zero values.
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.
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
- 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.
len(s) == 0, and not
s == nilfor checking for empty slice.
Iterating over slice
Iteration over slice is similar to array iteration. Preferred way is to
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
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.
When slice is passed to a function, it is passed by value like every other value in Go. Since slice is made up of only three parts : a pointer, a length, and a capacity. Passing slice between functions is inexpensive operation.
Backing array used to store slice data is shared between function calls internally.
hugeData is slice with
4KB size but when passed to other functions, only 3
word data structure representing slice is passed not the whole
4KB backing array.