小编shr*_*yas的帖子

为什么 go 中的切片对索引 1 的长度大于长度但不能更远

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:] 也应该出错。

go slice

6
推荐指数
1
解决办法
1325
查看次数

标签 统计

go ×1

slice ×1