Slices

by
Slices and loops in go and how to use them.

GrowSlice

Use append to show how the capacity of a slice grows. func TestSlice(t *testing.T) { s := make([]int, 0) last := cap(s) max := 20 for i := 0; i < max; i++ { s = append(s, i) if cap(s) != last { t.Logf("len(s)=%v cap(s)=%v\n", len(s), cap(s)) last = cap(s) } } exp := max if cap(s) != exp { t.Fail() } }