package main
import "fmt"
func main() {
is := []int{1, 2}
fmt.Println(is[2:]) // no panic here - this includes is[2] which is out of bound still no panic
fmt.Println(is[3:]) // but panic here
fmt.Println(is[2]) // panic here which is acceptable
}
在上面提到的程序中,即使我们正在从is [2]到on病房访问元素,并且片中只有2个元素,也没有为is [2:]感到恐慌。为什么会这样呢?
The go spec on slice expressions spells out the requirements for indices used in slicing:
As for index expressions, the relevant requirement is: