切片索引大于长度且小于容量会产生错误

发布于 2024-11-29 15:14:00 字数 910 浏览 1 评论 0原文

以下代码在运行时出现错误。

package main

import fmt "fmt"

func main(){

    type b []int
    var k = make([]b, 5, 10)
    fmt.Printf("%d\n",k[8])
    fmt.Printf("%d", len(k))
}

错误如下。

panic: runtime error: index out of range

runtime.panic+0x9e /go/src/pkg/runtime/proc.c:1060
        runtime.panic(0x453b00, 0x300203f0)
runtime.panicstring+0x94 /go/src/pkg/runtime/runtime.c:116
        runtime.panicstring(0x4af6c6, 0xc)
runtime.panicindex+0x26 /go/src/pkg/runtime/runtime.c:73
        runtime.panicindex()
main.main+0x8d C:/GOEXCE~1/basics/DATATY~1/slice.go:9
        main.main()
runtime.mainstart+0xf 386/asm.s:93
        runtime.mainstart()
runtime.goexit /go/src/pkg/runtime/proc.c:178
        runtime.goexit()
----- goroutine created by -----
_rt0_386+0xbf 386/asm.s:80

如果打印 k[0]k[1] ,则运行正常。您能否解释一下容量对于切片到底意味着什么?

Following code gives a error at runtime.

package main

import fmt "fmt"

func main(){

    type b []int
    var k = make([]b, 5, 10)
    fmt.Printf("%d\n",k[8])
    fmt.Printf("%d", len(k))
}

Error is as follows.

panic: runtime error: index out of range

runtime.panic+0x9e /go/src/pkg/runtime/proc.c:1060
        runtime.panic(0x453b00, 0x300203f0)
runtime.panicstring+0x94 /go/src/pkg/runtime/runtime.c:116
        runtime.panicstring(0x4af6c6, 0xc)
runtime.panicindex+0x26 /go/src/pkg/runtime/runtime.c:73
        runtime.panicindex()
main.main+0x8d C:/GOEXCE~1/basics/DATATY~1/slice.go:9
        main.main()
runtime.mainstart+0xf 386/asm.s:93
        runtime.mainstart()
runtime.goexit /go/src/pkg/runtime/proc.c:178
        runtime.goexit()
----- goroutine created by -----
_rt0_386+0xbf 386/asm.s:80

While if k[0] or k[1] is printed, it runs fine. Can you please explain what exactly capacity means for slices.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从﹋此江山别 2024-12-06 15:14:00

您只是简单地建立索引,因此索引必须小于长度。 Go 规范的相关部分 指出

形式的主要表达式

一个[x]

...

对于类型 A 或 *A(其中 A 是数组类型)的 a,或者对于类型 S 的 a
其中 S 是切片类型:

x 必须是整数值且 0 <= x <长度(a)

但是,如果您要“切片”(例如 a[6:9]),那么它将适用于大于长度但在容量范围内的索引。

You are simply indexing, so the index must be less than the length. The relevant section of the Go specification says that

A primary expression of the form

a[x]

...

For a of type A or *A where A is an array type, or for a of type S
where S is a slice type:

x must be an integer value and 0 <= x < len(a)

However, if you were "slicing" (e.g. a[6:9]), then it would work with indexes that are greater than the length but within the capacity.

夏雨凉 2024-12-06 15:14:00
var slice = make([]b, 5, 10)

等于

var array [10]b
slice := array[:5]

不同的是,当使用var slice = make([]b, 5, 10)时,无法访问slice下的arrayslice := array[:5] 表示 slice 的第一个元素是 array[0] 并且 slice< 的长度/code> 是 5,这意味着 slice[0] == array[0], slice[1] == array[1], ... slice[4] == array[4] 。因为你只能访问小于长度的索引(这意味着0 <=索引<长度)。 slice的长度为5,array的长度为10,因此可以访问array[8](0<=8<10 ) 但无法访问 slice[8](8>5)。

完整示例:

package main

import fmt "fmt"

func main(){

    type b []int
    var array [10]b
    slice := array[:5]
    // []
    fmt.Printf("%d\n",slice[1])
    // []
    fmt.Printf("%d\n",array[8])
    // panic: runtime error: index out of range
    fmt.Printf("%d\n",slice[8])
}

参考

  1. https://blog.golang.org/go-slices-使用和内部
var slice = make([]b, 5, 10)

is equal to

var array [10]b
slice := array[:5]

The difference is that when you use var slice = make([]b, 5, 10), you can't access the array under slice. The slice := array[:5] means the first element of slice is array[0] and the length of slice is 5, which means slice[0] == array[0], slice[1] == array[1], ... slice[4] == array[4]. Because you can only access the index that is less than the length(which means 0 <= index < length). The length of slice is 5 and the length of array is 10, so you can access array[8](0<=8<10) but can't access slice[8](8>5).

Full sample:

package main

import fmt "fmt"

func main(){

    type b []int
    var array [10]b
    slice := array[:5]
    // []
    fmt.Printf("%d\n",slice[1])
    // []
    fmt.Printf("%d\n",array[8])
    // panic: runtime error: index out of range
    fmt.Printf("%d\n",slice[8])
}

Reference

  1. https://blog.golang.org/go-slices-usage-and-internals
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文