Golang索引超出范围

huangapple go评论84阅读模式
英文:

Golang index out of range

问题

我正在尝试制作一个简单的Golang示例。这个示例在Golang之旅的第36页上。

这是我的代码:

package main

import "code.google.com/p/go-tour/pic"
import "fmt"

func Pic(dx, dy int) [][]uint8 {
    dx8 := uint8(dx)

    s1 := make([][]uint8, dy)
    fmt.Printf("%d\n", len(s1), cap(s1))

    for i := range s1 {
        fmt.Printf("%d\n", i)
        for j := range s1 {
            fmt.Printf("%d\n", j)
            s1[i][j] = dx8
        }
    }

    return s1
}

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

我得到的错误是:

256
%!(EXTRA int=256)0
0
panic: runtime error: index out of range

goroutine 1 [running]:
panic(0x18b820, 0x1040a010)
    /usr/local/go/src/runtime/panic.go:464 +0x700
main.Pic(0x100, 0x100, 0x0, 0x0, 0x0, 0x0)
    /tmp/sandbox716191956/main.go:17 +0x4e0
code.google.com/p/go-tour/pic.Show(0x1d7988, 0x104000e0)
    /go/src/code.google.com/p/go-tour/pic/pic.go:20 +0x60
main.main()
    /tmp/sandbox716191956/main.go:26 +0x20

Program exited.

我只想返回一个二维切片,供Show函数使用。

英文:

I'm trying to make a simply example of Golang. The example it's in te 36º page of the tour of golang.
This is my code:

package main

import "code.google.com/p/go-tour/pic"
import "fmt"

func Pic(dx, dy int) [][]uint8 {
    dx8 := uint8(dx)

    s1 := make ([][] uint8, dy)
    fmt.Printf("%d\n", len(s1), cap(s1))

    for i := range s1{
        fmt.Printf("%d\n",i)
        for j := range s1 {
            fmt.Printf("%d\n",j)
            s1[i][j] = dx8    
        }
    }

    return s1
}

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

And the error I got:

    256
%!(EXTRA int=256)0
0
panic: runtime error: index out of range

goroutine 1 [running]:
panic(0x18b820, 0x1040a010)
	/usr/local/go/src/runtime/panic.go:464 +0x700
main.Pic(0x100, 0x100, 0x0, 0x0, 0x0, 0x0)
	/tmp/sandbox716191956/main.go:17 +0x4e0
code.google.com/p/go-tour/pic.Show(0x1d7988, 0x104000e0)
	/go/src/code.google.com/p/go-tour/pic/pic.go:20 +0x60
main.main()
	/tmp/sandbox716191956/main.go:26 +0x20

Program exited.

I only want to return a bidimensial slice which Show function uses.

答案1

得分: 1

你需要为图片的两个维度 dy 和 dx 制作切片。例如,

package main

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

func Pic(dx, dy int) [][]uint8 {
    p := make([][]uint8, dy)
    for i := range p {
        p[i] = make([]uint8, dx)
        for j := range p[i] {
            p[i][j] = uint8(i * j)
        }
    }
    return p
}

func main() {
    pic.Show(Pic)
}
英文:

You need to make the slices for both dimensions, dy and dx, of the picture. For example,

package main

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

func Pic(dx, dy int) [][]uint8 {
	p := make([][]uint8, dy)
	for i := range p {
		p[i] = make([]uint8, dx)
		for j := range p[i] {
			p[i][j] = uint8(i * j)
		}
	}
	return p
}

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

答案2

得分: 0

你的代码中的问题是在第二个循环中循环的大小错误。
你的第二个循环的范围应该是s1[i]的大小,而不是s1,因为第二个循环处理的是内部维度。

for i := range s1{
    fmt.Printf("%d\n",i)
    s1[i] = make([]uint8, dy)
    for j := range s1[i] {
        fmt.Printf("%d\n",j)
        s1[i][j] = dx8

    }
}
英文:

The problem in your code you are looping in wrong size in the second loop.
your second range must be sieze of s1[i] not s1 because the second loop processes on inner dimension.

for i := range s1{
    fmt.Printf("%d\n",i)
    s1[i] = make ([] uint8, dy)
    for j := range s1[i] {
        fmt.Printf("%d\n",j)
        s1[i][j] = dx8

    }
}

huangapple
  • 本文由 发表于 2016年3月28日 19:03:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/36260994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定