Append and Capacity
A slice made with make([]int, 3) has 3 elements. But often you do not know how many values you will keep until the work is done. The built-in function append adds values to the end of a slice, and the slice grows.
package main
import "fmt"
func main() {
var names []string
fmt.Println(names, len(names))
names = append(names, "Alice")
names = append(names, "Bob", "Emma")
fmt.Println(names, len(names))
}
Output:
[] 0
[Alice Bob Emma] 3
var names []string declares a slice with no elements and no underlying array yet. Go calls such a slice nil. You do not need to make anything first, because append works on a nil slice.
append takes a slice and one or more values. It does not change your variable. It returns a slice with the new values at the end, and you must store that result, usually back in the same variable. If you forget, the compiler stops you:
package main
import "fmt"
func main() {
ids := []int{11, 12}
append(ids, 13)
fmt.Println(ids)
}
Output:
# command-line-arguments
./main.go:7:2: append(ids, 13) (value of type []int) is not used
Collecting results in a loop
The most common use of append is this pattern: start with an empty slice, and append each value you want to keep. This loop reads 5 numbers and keeps the even ones. Here the input is 4 7 10 3 8:
var evens []int
for i := 0; i < 5; i++ {
var x int
fmt.Scan(&x)
if x%2 == 0 {
evens = append(evens, x)
}
}
fmt.Println(evens)
Output:
[4 10 8]
Length and capacity
How can a slice grow, when its underlying array has a fixed size? The underlying array often has more room than the slice uses. The capacity of a slice, cap(s), is how many elements fit in its underlying array. When you append and there is room, Go puts the value into the free space. When the array is full, Go makes a new, bigger array, copies the old elements into it, and the slice refers to the new array.
var s []int
for i := 1; i <= 5; i++ {
s = append(s, i)
fmt.Println(len(s), cap(s))
}
Output:
1 1
2 2
3 4
4 4
5 8
Here the capacity went from 1 to 2, then 4, then 8. Go decides how much room to add, and the rule can change between Go versions, so never write code that depends on an exact capacity.
If you know how many values are coming, give make a third number, the capacity. The slice starts empty but has room ready, so Go does not need to copy:
s := make([]int, 0, 5)
fmt.Println(s, len(s), cap(s))
s = append(s, 7, 8)
fmt.Println(s, len(s), cap(s))
Output:
[] 0 5
[7 8] 2 5
append inside a function
The Slice Basics lesson showed that a function can change the elements of the caller's slice. Appending in a function is different:
package main
import "fmt"
func addOne(nums []int) {
nums = append(nums, 1)
fmt.Println("inside:", nums)
}
func main() {
nums := []int{5, 6}
addOne(nums)
fmt.Println("outside:", nums)
}
Output:
inside: [5 6 1]
outside: [5 6]
The function's nums is its own variable. append stored a new, longer slice in it, but the nums in main still has length 2. To give the caller the new slice, return it and store it:
package main
import "fmt"
func addOne(nums []int) []int {
return append(nums, 1)
}
func main() {
nums := []int{5, 6}
nums = addOne(nums)
fmt.Println(nums)
}
Output:
[5 6 1]
This is the same rule as before: always store what append returns.
In the exercise, use append to collect the order amounts that are 50 or more.