A past puzzle — fully playable. 4 attempts, hints on wrong guesses.
stdle-117.go
1packagemain
2
3import"fmt"
4
5funcmain(){
6a:=[]int{0,1,2,3,4,5}
7b:=a[2:4]
8fmt.Println(len(b),cap(b))
9c:=b[:cap(b)]
10fmt.Println(c)
11}
Slices
Answer & explanation
Console output
2 4
[2 3 4 5]
Why
For b := a[2:4], capacity is measured from the low index to the end of the underlying array, so cap(b) is 6-2 = 4 while len(b) is 2. Reslicing b[:cap(b)] extends b to its full capacity, exposing the trailing elements [2 3 4 5]. This idiom recovers all backing storage reachable from a slice’s start.