Back to questions
JuniorTheoryVery common
What does the program print?
package main
import "fmt"
func main() {
s := []int{1, 2}
t := append(s, 3)
t[0] = 9
fmt.Println(s[0], len(s), len(t))
}1 2 3
- Expecting
9 2 3:shas no free capacity, soappendcopies the elements into a new array, andt[0] = 9changes only that copy. - Expecting
1 3 3or an error:appendnever changessitself, and a full slice is not an error, it just moves to a bigger array.
- What growth factor does Go use when
appendreallocates, and why? - After
appendreallocates, do earlier slices still see the change?