duanchen7703 2017-01-07 16:02
浏览 221
已采纳

golang:为什么以下代码不会因“切片索引超出范围”错误而惊慌[重复]

This question already has an answer here:

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
", 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
", 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?

</div>
  • 写回答

1条回答 默认 最新

  • dongpu8935 2017-01-07 17:48
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据