2 min read
Range of slice expressions
Simple slice expressions … For arrays or strings, the indices are in range if 0 <= low <= high <= len(a), otherwise they are out of range. This is probably intuitive. a := [5]int{1, 2, 3, 4, 5} s := "Abracadabra" a[-1] // Out of range s[5:1] // Out of range a[12] // Out of range s[999] // Out of range For slices, the upper index bound is the slice capacity cap(a) rather than the length.
1 min read
Quiz
Today’s quiz day. Tomorrow I’ll provide the answer as we continue with the section of the spec that explains today’s code. Consider the following program. What is its output? package main import "fmt" func main() { a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} s := a[2:5] s2 := s[5:7] fmt.Println(s2) } [6 7] [8 9] A runtime panic A compilation error