为什么go中的切片对索引1的作用大于长度而不是更远

为什么go中的切片对索引1的作用大于长度而不是更远,go,slice,Go,Slice,有人能解释为什么s[2:]返回空切片[],但s[3:]出错。我认为s[2:]也应该出错。基于 对于数组或字符串,如果为0,则索引在范围内 0 1 2 n-1 n |_____|_____|_..._|_____| s[0]s[1]s[n-1] 我从一本戈兰书中找到了这个解释。上面是指数,下面是元素 我们可以看到,在获取子片时,n也是一个有效的索引,这可以解释为什么s[a:b]={s[a],s[a+1],…,s[b-1]},和s[n:]=s[n:n]={} package main import

有人能解释为什么s[2:]返回空切片[],但s[3:]出错。我认为s[2:]也应该出错。

基于

对于数组或字符串,如果为0,则索引在范围内 0 1 2 n-1 n |_____|_____|_..._|_____| s[0]s[1]s[n-1] 我从一本戈兰书中找到了这个解释。上面是指数,下面是元素

我们可以看到,在获取子片时,n也是一个有效的索引,这可以解释为什么
s[a:b]={s[a],s[a+1],…,s[b-1]}
,和
s[n:]=s[n:n]={}

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
}
0 1 2 n-1 n |_____|_____|_..._|_____| s[0] s[1] s[n-1]