A Go slice is a segment of an array.
Like arrays, slices are indexable and have a length.
Unlike arrays, this length of a slice is allowed to change.
Here's an example of a slice:
var x []float64
The line above is missing length between the brackets.
In this case, x has been created with a length of zero.
To create a slice, use the built-in make function:
x := make([]float64, 5)
This creates a slice that is associated with an underlying float64 array of length 5.
The make function also allows a third parameter:
x := make([]float64, 5, 10)
10 represents the capacity of the underlying array that the slice points to:
Another way to create slices is to use the [low : high] expression:
arr := [5]float64{1,2,3,4,5}
x := arr[0:5]
low is the index of where to start the slice and high is the index of where to end it, but not including the index itself.
For example, arr[0:5] returns [1,2,3,4,5], arr[1:4] returns [2,3,4].
For convenience, we can omit low, high, or even both low and high.
arr[0:] is the same as arr[0:len(arr)].
arr[:5] is the same as arr[0:5].
arr[:] is the same as arr[0:len(arr)].