英文:
golang: Why the following code does NOT panic with "slice index out of range" error
问题
以下是翻译好的内容:
package main
import "fmt"
type Point struct {
X int
Y int
}
type Points struct {
P []Point
}
func main() {
data := Points{}
for i := 0; i < 10; i++ {
data.P = append(data.P, Point{
X: i,
Y: i*2,
})
}
fmt.Printf("%+v\n", data.P[5:11]);
}
在运行上述程序时,它打印出:
[{X:5 Y:10} {X:6 Y:12} {X:7 Y:14} {X:8 Y:16} {X:9 Y:18} {X:0 Y:0}]
为什么会有 {X:0, Y:0},它似乎是自动生成的,因为切片的长度是10,但我尝试获取的是 5:11?
我在代码中找到了问题,并使用 "raw" 切片进行了测试,如下所示:
package main
import "fmt"
func main() {
v := []int{0,1,2,3,4,5,6,7,8,9}
fmt.Printf("%v\n", v[5:11])
}
这个简单的程序生成了错误(如预期的):
panic: runtime error: slice bounds out of range
goroutine 1 [running]:
panic(0x47a8e0, 0xc42000a130)
/usr/local/go/src/runtime/panic.go:500 +0x1a1
main.main()
/home/fxr/go/src/cmhwc/test2.go:7 +0x53
exit status 2
为什么第一个程序没有发生 panic?
英文:
package main
import "fmt"
type Point struct {
X int
Y int
}
type Points struct {
P []Point
}
func main() {
data := Points{}
for i := 0; i < 10; i++ {
data.P = append(data.P, Point{
X: i,
Y: i*2,
})
}
fmt.Printf("%+v\n", data.P[5:11]);
}
While the above program is run, it printed out:
[{X:5 Y:10} {X:6 Y:12} {X:7 Y:14} {X:8 Y:16} {X:9 Y:18} {X:0 Y:0}]
Why there are {X:0, Y:0} which seems to be automatically generated as the slice's length is 10 but I try to get 5:11?
I found the problem in my code and test with "raw" slice such as:
package main
import "fmt"
func main() {
v := []int{0,1,2,3,4,5,6,7,8,9}
fmt.Printf("%v\n", v[5:11])
}
This simple program generates errors (as expected):
panic: runtime error: slice bounds out of range
goroutine 1 [running]:
panic(0x47a8e0, 0xc42000a130)
/usr/local/go/src/runtime/panic.go:500 +0x1a1
main.main()
/home/fxr/go/src/cmhwc/test2.go:7 +0x53
exit status 2
Why the first program does not panic?
答案1
得分: 3
因为当你使用append()向一个没有足够空间添加新元素的切片中添加元素时,它会创建一个新的切片,将容量加倍,并复制旧元素和新元素,然后返回对新切片的引用。当你使用切片语法请求超出切片末尾的元素时,实际上是将长度应用于由append创建并返回的底层数组。
如@JimB在评论中建议的那样,你可以添加一个语句在每次追加循环中打印容量,以查看这个过程:
fmt.Println(cap(data.P))
应该会生成类似以下的输出:
2
2
4
4
8
8
8
8
16
16
英文:
Because when you use append() to add elements to a slice which has no more room for adding the new elements it creates a new one doubling its capacity copying the old elements as well as the new you're asking to add and returning a reference to that. Further down when you use slice syntax to ask for elements past the apparent end of the slice the length is actually applied to the underlying array created & returned by append.
As @JimB suggested in the comments, you can add a statement to print the capacity at every step in the append loop to see this happening:
fmt.Println(cap(data.P))
Should generate something like:
2
2
4
4
8
8
8
8
16
16
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论