package main
import (
"fmt"
)
func main() {
s:= []int{1,2}
fmt.Println(s[0:]) // this is expected
fmt.Println(s[1:]) // this is expected
fmt.Println(s[2:]) // this wondering why i didn't recieve slice bounds out of range error
fmt.Println(s[3:]) // here i recieve the error
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么 s[2:] 返回空切片 [] 但 s[3:] 出错。我认为 s[2:] 也应该出错。