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:] 没有恐慌,即使我们正在访问从 is[2] 到 on 病房的元素并且切片只有 2 个元素。为什么会这样?
湖上湖
相关分类