去游览,练习:切片指数超出范围 [英] Go Tour, Excercise:Slices Index out of Range

查看:114
本文介绍了去游览,练习:切片指数超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Go语言游的练习中工作,我遇到了一个我无法弄清的障碍。我正在做练习:切片,我得到这个错误:

  256 x 256 

panic:运行时错误:索引超出范围

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

以下是我的代码:

 包主

导入tour / pic

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

像素:= 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)
}
}

返回像素
}

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



<对于我的生活,我找不到这个问题!

解决方案


切片



对于字符串,数组,指向数组或切片a,主
表达式

a [low:high]

构造子字符串或切片。索引表达式低位和高位
选择哪些元素出现在结果中。结果索引
从0开始,长度等于高 - 低。


对于数组或字符串,索引low和high必须满足0 <= low
<= high <= length;对于切片,上限是容量而不是长度。

索引



表单的主要表达形式

a [b]

表示由x索引的数组,切片,字符串或映射的元素。
值x分别被称为索引或映射键。以下规则适用于
规则:


对于A或A类型,其中A是数组类型或S
类型,其中S是切片类型:

  x必须是整数值并且0 <= x < len(a)

a [x]是索引x处的数组元素,a [x]的类型是
A

的元素类型,如果a是零,或者如果索引x超出范围,运行时恐慌发生

制作切片,地图和频道

 (T,n)长度为n且容量为n 
的T型片段(T,n,m)长度为n且容量为m
的T型片段


y必须是整数值并且0 <= y < LEN(像素[] UINT8)。 x必须是整数值并且0 <= x < LEN(像素[] [] UINT8)。例如,

 包主

导入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)
}
}
返回像素
}

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


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\n\n", 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!

解决方案

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)
}

这篇关于去游览,练习:切片指数超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆