1 /*
2 pop is the slice mutator's other half, and it is a STATEMENT only:
3 4 pop(s) removes the last element
5 pop(s, xs...) removes len(xs) elements - the mirror of push(s, xs...)
6 7 There is no read form: `s[len(s)-1]` is the read and `s[len(s)-1:]` is the
8 copy. `v := pop(s)` is a compile error - it used to return the value without
9 shrinking, which meant two different things depending on whether the value was
10 used.
11 */
12 package main
13 14 func main() {
15 select {
16 case <-shutdown:
17 return
18 default:
19 }
20 s := []string{"a", "b", "c", "d"}
21 pop(s)
22 if len(s) != 3 || s[2] != "c" {
23 panic("pop(s) did not remove the last element")
24 }
25 drop := []string{"x", "y"}
26 pop(s, drop...)
27 if len(s) != 1 || s[0] != "a" {
28 panic("pop(s, xs...) did not remove len(xs)")
29 }
30 last := s[len(s)-1]
31 pop(s)
32 if last != "a" || len(s) != 0 {
33 panic("read then pop on a one-element slice")
34 }
35 k := []int32{5, 6}
36 v := k[len(k)-1]
37 if v != 6 || len(k) != 2 {
38 panic("reading the last element must not shrink")
39 }
40 pop(k)
41 if len(k) != 1 {
42 panic("pop did not shrink")
43 }
44 println("pop forms ok")
45 }
46