dongtun3328 2012-07-20 18:31
浏览 42
已采纳

进行锻炼,运动:切片索引超出范围

I am working through the excercises in the Go language tour and I have hit a snag that I can't figure out. I'm doing Exercise: Slices and I am getting this error:

256 x 256

panic: runtime error: index out of range

goroutine 1 [running]:
main.Pic(0x10000000100, 0x3, 0x417062, 0x4abf70)
    /tmpfs/gosandbox-08a27793_4ffc9f4a_3b917355_ef23793d_c15d58cc/prog.go:9 +0xa0
tour/pic.Show(0x400c00, 0x40caa2)
    go/src/pkg/tour/pic/pic.go:20 +0x2d
main.main()
    /tmpfs/gosandbox-08a27793_4ffc9f4a_3b917355_ef23793d_c15d58cc/prog.go:20 +0x25

Here is my code:

package main

import "tour/pic"

func Pic(dx, dy int) [][]uint8 {
    fmt.Printf("%d x %d

", dx, dy)

pixels := make([][]uint8, 0, dy)

for y := 0; y < dy; y++ {
    pixels[y] = make([]uint8, 0, dx)

    for x := 0; x < dx; x++ {
        pixels[y][x] = uint8(x*y)
    }
}

return pixels
}

func main() {
    pic.Show(Pic)
}

For the life of me I can't find the issue!

  • 写回答

2条回答 默认 最新

  • dongyou26216708 2012-07-20 19:02
    关注

    Slices

    For a string, array, pointer to array, or slice a, the primary expression

    a[low : high]

    constructs a substring or slice. The index expressions low and high select which elements appear in the result. The result has indexes starting at 0 and length equal to high - low.

    For arrays or strings, the indexes low and high must satisfy 0 <= low <= high <= length; for slices, the upper bound is the capacity rather than the length.

    Indexes

    A primary expression of the form

    a[x]

    denotes the element of the array, slice, string or map a indexed by x. The value x is called the index or map key, respectively. The following rules apply:

    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)
    
    a[x] is the array element at index x and the type of a[x] is
    the element type of A
    
    if a is nil or if the index x is out of range, a run-time panic occurs
    

    Making slices, maps and channels

    make(T, n)       slice      slice of type T with length n and capacity n
    make(T, n, m)    slice      slice of type T with length n and capacity m
    

    y must be an integer value and 0 <= y < len(pixel[]uint8). x must be an integer value and 0 <= x < len(pixel[][]uint8). For example,

    package main
    
    import "tour/pic"
    
    func Pic(dx, dy int) [][]uint8 {
        pixels := make([][]uint8, dy)
        for y := 0; y < dy; y++ {
            pixels[y] = make([]uint8, dx)
            for x := 0; x < dx; x++ {
                pixels[y][x] = uint8(x * y)
            }
        }
        return pixels
    }
    
    func main() {
        pic.Show(Pic)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题