Slice Basics
An array has a fixed size. But most of the time, you do not know in advance how many values you will get. A slice holds values of one type in order, just like an array, but its length is not part of its type. The slice is the list you will use most in Go.
package main
import "fmt"
func main() {
scores := []int{70, 95, 60}
fmt.Println(scores)
fmt.Println(len(scores))
fmt.Println(scores[0])
}
Output:
[70 95 60]
3
70
In the code, the only visible difference from an array is the empty square brackets. []int is a slice of int values, and [3]int is an array of exactly 3. Indexes, len and changing an element with scores[1] = 90 all work the same way as with arrays.
The last element is always at index len(scores)-1, because indexes start at 0. You will use this often:
scores := []int{70, 95, 60}
last := scores[len(scores)-1]
fmt.Println("Last:", last)
Output:
Last: 60
Making a slice with make
When the size comes from input, use the built-in function make. make([]int, n) creates a slice of n ints, and every element starts as 0. Here the input is 3 on the first line and 4 8 15 on the second:
var n int
fmt.Scan(&n)
nums := make([]int, n)
fmt.Println(nums)
for i := 0; i < n; i++ {
fmt.Scan(&nums[i])
}
fmt.Println(nums)
Output:
[0 0 0]
[4 8 15]
An array cannot do this, because the size of an array must be a constant. A slice can have any length, even one that is only known when the program runs.
Like an array, a slice checks every index. An index that does not exist stops the program with a panic:
scores := []int{70, 95}
i := 5
fmt.Println(scores[i])
Output:
panic: runtime error: index out of range [5] with length 2
Slices share their elements
Here is the big difference from arrays. A slice does not keep its elements itself. Go stores them in an array in the background, called the underlying array, and the slice refers to that array. When you assign a slice to another variable, both variables refer to the same underlying array:
a := []int{1, 2, 3}
b := a
b[0] = 100
fmt.Println(a)
fmt.Println(b)
Output:
[100 2 3]
[100 2 3]
There is only one underlying array, and both a and b refer to it:
a ---+
+---> [ 100 | 2 | 3 ] underlying array
b ---+
In the Arrays lesson, changing the copy left the original alone. With slices, there is no copy of the elements, so the change shows in both.
The same happens when you pass a slice to a function. The parameter is still a copy, as the Variable Scope lesson said, but it is a copy of the slice, and the copy refers to the same underlying array. So the function can change the caller's elements:
package main
import "fmt"
func double(nums []int) {
for i := 0; i < len(nums); i++ {
nums[i] = nums[i] * 2
}
}
func main() {
prices := []int{5, 10, 20}
double(prices)
fmt.Println(prices)
}
Output:
[10 20 40]
This is useful. A function can fill or change a long list without copying it. But remember it when you pass a slice to a function that should not change your data.
In the exercise, make a slice with make, read the order IDs into it, and print the slice, the first ID and the last ID.