简体   繁体   English

在Golang中,为什么这样的类型转换会导致运行时错误:索引超出范围?

[英]In Golang, why such Type Conversion causes Runtime Error: index out of range?

I was doing the exercise of "A Tour of Go", the page I was on is https://tour.golang.org/moretypes/15 我正在练习“ A Go of Go”,我浏览的页面是https://tour.golang.org/moretypes/15

And following is my code: 以下是我的代码:

package main

import "golang.org/x/tour/pic"

func Pic(dx, dy int) [][]uint8 {
    var ret  [][]uint8;
    var row []uint8;
    for i:=uint8(0);i<uint8(dy);i++ {
        row = []uint8 {}
        for j:=uint8(0);j<uint8(dx);j++ {
            row = append(row, i+j)
        }
        ret = append(ret, row)
    }
    return  ret
}

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

When I run these codes, the console throws an error: 当我运行这些代码时,控制台将引发错误:

panic: runtime error: index out of range

goroutine 1 [running]:
panic(0x18b820, 0x1040a010)
    /usr/local/go/src/runtime/panic.go:464 +0x700
golang.org/x/tour/pic.Show(0x1d7948, 0x104000e0)
    /go/src/golang.org/x/tour/pic/pic.go:24 +0x540
main.main()
    /tmp/sandbox969725880/main.go:19 +0x20

Does anyone have any ideas why does the error occur for this int->uint8 type conversion?Thanks! 有谁知道为什么此int-> uint8类型转换会发生错误?

uint8 has a max value of 255 (only 8 bits, max 2^8) but dx, dy passed into Pic can have values greater than that (since they're int's, likely 64 bits). uint8的最大值为255(仅8位,最大2 ^ 8),但是传递给Pic的dx,dy可以具有更大的值(因为它们是int的,可能是64位)。 Values greater than 255 might get cast to 0 during conversion to uint8. 转换为uint8时,大于255的值可能会强制转换为0。 If dy is 256 and it's cast to 0 as i, the outer for loop doesn't execute at all and no items get pushed into the array. 如果dy是256并且将其强制转换为i,则外部for循环根本不会执行,并且不会有任何项目被压入数组。 Subsequently, when whatever mechanism in "golang.org/x/tour/pic" tries to access the values in the matrix after it's returned, it looks like it's generating an 'index out of range' error because there are literally no indexes in the matrix to access. 随后,当“ golang.org/x/tour/pic”中的任何机制尝试在返回矩阵后尝试访问矩阵中的值时,它似乎正在生成“索引超出范围”错误,因为矩阵访问。

Working code: 工作代码:

package main

import "golang.org/x/tour/pic"

func Pic(dx, dy int) [][]uint8 {
    var ret [][]uint8

    for i := 0; i < dy; i++ {
        row := []uint8{}
        for j := 0; j < dx; j++ {
            row = append(row, uint8(i+j))
        }
        ret = append(ret, row)
    }
    return ret
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导致恐慌的原因:运行时错误:索引超出范围 [4],长度为 4,即使数组已初始化为动态数组 - What causes panic: runtime error: index out of range [4] with length 4 even though array is initialized as dynamic array 紧急运行时错误索引超出范围 [3],长度为 3 - Panic Runtime Error index out of range [3] with length 3 在golang中使用make方法创建2D数组时,我遇到问题“紧急:运行时错误:索引超出范围” - i am facing issue “panic: runtime error: index out of range” when create 2D array with make method in golang 我在for循环“恐慌:运行时错误:索引超出范围”中迭代arrary时遇到这种类型的错误 - I have this type of error while iterate arrary in for loop "panic: runtime error: index out of range" 测试指数超出范围golang - Test Index out of range golang Golang嵌套结构片-错误索引超出范围 - Golang nested struct slice - Error index out of range Swift 5:为什么会出现Index out of range错误? - Swift 5: Why the Index out of range error appears? 数组 - 运行时错误:索引超出类型“int [*]”的范围 - Array - runtime error: index out of bounds for type 'int [*]' 运行时错误:类型“int [*]”的索引 2 超出范围,leetcode Palindrome - runtime error: index 2 out of bounds for type 'int [*]', leetcode Palindrome 为什么我得到线程 1:致命错误:索引超出范围? - Why am I getting Thread 1: Fatal error: Index out of range?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM