Go Slices
Links: 103 Golang Index
Another key difference between them is that arrays always passes by value and slices always passed by reference. It's a reason for better performance while using slices.
Slices¶
- Dynamic length
- Similarities between array and slice
- Both a slice and an array can contain only the same type of elements
- We can create a keyed slice like a keyed array
- Differences between array and slice
- The length of an array is part of its type, defined at compile time and cannot be changed. The length of a slice is not part of its type and it belongs to runtime.
- By default an uninitialised array has all elements equal to zero. An uninitialised slice is equal to nil (its zero value is nil).
nildoesn't mean the absence of a value but the value hasn't been initialised yet.- A
nilslice is an empty slice with 0 capacity. - This is why we can call the
lenfunction on an uninitialised slice. - We cannot assign elements to a
nilslice
- A
- Slices cannot be compared with the
=operator, they can only be compared to nil.- To compare 2 slices we use a for loop to iterate over the slices and compare element by element.
Creating a slice¶
- Uninitialised slice equal to nil
var values []int; fmt.Println(value == nil)- true.- Declare a slice but don't allocated memory just yet
- Initialised slice not equal to nil
values := []int{}; fmt.Println(values == nil)- false.- Initialise but with no values, memory is allocated
values := []int{1,2,3}var values = []int{1,2,3}- Using
make:make(type, len, capacity)values := make([]int, 2)- same as[]int{0, 0}- The default values are used to initialise the array.
- Another way :
type names []string; friends := names{"Dan", "Maria"}
Appending values to a slice¶
- We add values to a slice using
append. - This returns a new slice with the value appended to it. It doesn't modify the existing slice.
- The new slice can be saved back in the same variable.
In the above example we can't use simple = since values2 hasn't been declared. If you were assigning the values1 to append then using = would have been okay
appendis a variadic function and can append more than one value.values = append(values, 20,30,40)
- Another use case of
appendis when we want to append one slice to another slice but we need...the end.
Copying Slices¶
copy creates a separate underlying array for the destination slice
- Copying one slice to another using
copy(dstSlice, srcSlice) - It copies the src slice to destination and returns the number of elements copied
- When source and destination are of the same length
- When source and destination are of different length
- We can't copy slices using
:=or=(if the array has been declared) since they will share the same backing array. - If destination is bigger than source then the beginning elements are overwritten
Slicing¶
- It doesn't modify the original slice/array but instead returns a new one.
- It works for both arrays and slices.
- Same rules as that of Python slicing.
- Missing start index defaults to 0.
- Missing end index defaults to maximum length.
- Element at the start index is included and element at the end index is not included.
- Slicing can be used for copying slices but they will share the same backing array.
- Some slice examples
- Removing elements
Go - Slice Working¶
Last updated: 2022-05-28