Golang索引超出范围

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

Golang index out of range

问题

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

这是我的代码:

  1. package main
  2. import "code.google.com/p/go-tour/pic"
  3. import "fmt"
  4. func Pic(dx, dy int) [][]uint8 {
  5. dx8 := uint8(dx)
  6. s1 := make([][]uint8, dy)
  7. fmt.Printf("%d\n", len(s1), cap(s1))
  8. for i := range s1 {
  9. fmt.Printf("%d\n", i)
  10. for j := range s1 {
  11. fmt.Printf("%d\n", j)
  12. s1[i][j] = dx8
  13. }
  14. }
  15. return s1
  16. }
  17. func main() {
  18. pic.Show(Pic)
  19. }

我得到的错误是:

  1. 256
  2. %!(EXTRA int=256)0
  3. 0
  4. panic: runtime error: index out of range
  5. goroutine 1 [running]:
  6. panic(0x18b820, 0x1040a010)
  7. /usr/local/go/src/runtime/panic.go:464 +0x700
  8. main.Pic(0x100, 0x100, 0x0, 0x0, 0x0, 0x0)
  9. /tmp/sandbox716191956/main.go:17 +0x4e0
  10. code.google.com/p/go-tour/pic.Show(0x1d7988, 0x104000e0)
  11. /go/src/code.google.com/p/go-tour/pic/pic.go:20 +0x60
  12. main.main()
  13. /tmp/sandbox716191956/main.go:26 +0x20
  14. 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:

  1. package main
  2. import "code.google.com/p/go-tour/pic"
  3. import "fmt"
  4. func Pic(dx, dy int) [][]uint8 {
  5. dx8 := uint8(dx)
  6. s1 := make ([][] uint8, dy)
  7. fmt.Printf("%d\n", len(s1), cap(s1))
  8. for i := range s1{
  9. fmt.Printf("%d\n",i)
  10. for j := range s1 {
  11. fmt.Printf("%d\n",j)
  12. s1[i][j] = dx8
  13. }
  14. }
  15. return s1
  16. }
  17. func main() {
  18. pic.Show(Pic)
  19. }

And the error I got:

  1. 256
  2. %!(EXTRA int=256)0
  3. 0
  4. panic: runtime error: index out of range
  5. goroutine 1 [running]:
  6. panic(0x18b820, 0x1040a010)
  7. /usr/local/go/src/runtime/panic.go:464 +0x700
  8. main.Pic(0x100, 0x100, 0x0, 0x0, 0x0, 0x0)
  9. /tmp/sandbox716191956/main.go:17 +0x4e0
  10. code.google.com/p/go-tour/pic.Show(0x1d7988, 0x104000e0)
  11. /go/src/code.google.com/p/go-tour/pic/pic.go:20 +0x60
  12. main.main()
  13. /tmp/sandbox716191956/main.go:26 +0x20
  14. Program exited.

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

答案1

得分: 1

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

  1. package main
  2. import "golang.org/x/tour/pic"
  3. func Pic(dx, dy int) [][]uint8 {
  4. p := make([][]uint8, dy)
  5. for i := range p {
  6. p[i] = make([]uint8, dx)
  7. for j := range p[i] {
  8. p[i][j] = uint8(i * j)
  9. }
  10. }
  11. return p
  12. }
  13. func main() {
  14. pic.Show(Pic)
  15. }
英文:

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

  1. package main
  2. import "golang.org/x/tour/pic"
  3. func Pic(dx, dy int) [][]uint8 {
  4. p := make([][]uint8, dy)
  5. for i := range p {
  6. p[i] = make([]uint8, dx)
  7. for j := range p[i] {
  8. p[i][j] = uint8(i * j)
  9. }
  10. }
  11. return p
  12. }
  13. func main() {
  14. pic.Show(Pic)
  15. }

答案2

得分: 0

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

  1. for i := range s1{
  2. fmt.Printf("%d\n",i)
  3. s1[i] = make([]uint8, dy)
  4. for j := range s1[i] {
  5. fmt.Printf("%d\n",j)
  6. s1[i][j] = dx8
  7. }
  8. }
英文:

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.

  1. for i := range s1{
  2. fmt.Printf("%d\n",i)
  3. s1[i] = make ([] uint8, dy)
  4. for j := range s1[i] {
  5. fmt.Printf("%d\n",j)
  6. s1[i][j] = dx8
  7. }
  8. }

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:

确定