A past puzzle — fully playable. 4 attempts, hints on wrong guesses.
stdle-158.go
1packagemain
2
3import"fmt"
4
5funcmain(){
6s:=make([]int,2,5)
7t:=append(s,1)
8s=append(s,2)
9fmt.Println(t)
10}
Slices
Answer & explanation
Console output
[0 0 2]
Why
s has len 2 and cap 5, so both appends have room and write into index 2 of the same backing array. t = append(s, 1) puts 1 there, then s = append(s, 2) overwrites it with 2. t’s header still has len 3 and reads the shared slot, which now holds 2, giving [0 0 2].