Go之旅练习#18:切片,索引超出范围

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

Tour of Go exercise #18: Slices, index out of range

问题

我正在完成Go之旅中的练习,但遇到了一个我无法解决的问题。

我正在进行切片练习,但是我遇到了以下错误:

256 x 256

panic: 运行时错误: 索引超出范围 [0],长度为0

goroutine 1 [running]:
main.Pic(0x100, 0x100)
	/tmp/sandbox1628012103/prog.go:14 +0xcf
golang.org/x/tour/pic.Show(0xc0000001a0)
	/tmp/gopath962180923/pkg/mod/golang.org/x/tour@v0.0.0-20201207214521-004403599411/pic/pic.go:32 +0x28
main.main()
	/tmp/sandbox1628012103/prog.go:25 +0x25

这是我的代码:

package main

import (
	"fmt"
	"golang.org/x/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)
}
英文:

I am working through the exercises in the Go 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 [0] with length 0

goroutine 1 [running]:
main.Pic(0x100, 0x100)
	/tmp/sandbox1628012103/prog.go:14 +0xcf
golang.org/x/tour/pic.Show(0xc0000001a0)
	/tmp/gopath962180923/pkg/mod/golang.org/x/tour@v0.0.0-20201207214521-004403599411/pic/pic.go:32 +0x28
main.main()
	/tmp/sandbox1628012103/prog.go:25 +0x25

Here is my code:

package main

import (
	&quot;fmt&quot;
	&quot;golang.org/x/tour/pic&quot;
)

func Pic(dx, dy int) [][]uint8 {
	fmt.Printf(&quot;%d x %d\n\n&quot;, dx, dy)

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

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

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

	return pixels
}

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

答案1

得分: 7

> 切片
>
> 对于字符串、数组、数组指针或切片 a,主要表达式
>
> a[low : high]
>
> 构造一个子字符串或切片。索引表达式 low 和 high 选择出现在结果中的元素。结果的索引从 0 开始,长度等于 high - low。
>
> 对于数组或字符串,索引 low 和 high 必须满足 0 <= low <= high <= length;对于切片,上限是容量而不是长度。
>
> 索引
>
> 形式为
>
> a[x]
>
> 的主要表达式表示数组、切片、字符串或映射 a 中由 x 索引的元素。值 x 称为索引或映射键。以下规则适用:
>
> 对于类型为 A 或 *A(其中 A 是数组类型)的 a,或者对于类型为 S(其中 S 是切片类型)的 a:
>
> x 必须是整数值,且 0 <= x < len(a)
>
> a[x] 是索引为 x 的数组元素,a[x] 的类型是 A 的元素类型
>
> 如果 a 是 nil 或者索引 x 超出范围,将发生运行时恐慌
>
> 创建切片、映射和通道
>
> make(T, n) 切片 类型为 T,长度为 n,容量为 n 的切片
> make(T, n, m) 切片 类型为 T,长度为 n,容量为 m 的切片
>

y 必须是整数值,且 0 <= y < len(pixel[]uint8)。x 必须是整数值,且 0 <= x < len(pixel[][]uint8)。例如,

package main

import &quot;tour/pic&quot;

func Pic(dx, dy int) [][]uint8 {
	pixels := make([][]uint8, dy)
	for y := 0; y &lt; dy; y++ {
		pixels[y] = make([]uint8, dx)
		for x := 0; x &lt; dx; x++ {
			pixels[y][x] = uint8(x * y)
		}
	}
	return pixels
}

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

> 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 &quot;tour/pic&quot;

func Pic(dx, dy int) [][]uint8 {
	pixels := make([][]uint8, dy)
	for y := 0; y &lt; dy; y++ {
		pixels[y] = make([]uint8, dx)
		for x := 0; x &lt; dx; x++ {
			pixels[y][x] = uint8(x * y)
		}
	}
	return pixels
}

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

答案2

得分: -2

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] = append(pixels[y], uint8(x*y))
}

return pixels

}

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

英文:
package main

import &quot;tour/pic&quot;

func Pic(dx, dy int) [][]uint8 {
fmt.Printf(&quot;%d x %d\n\n&quot;, dx, dy)

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

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

for x := 0; x &lt; dx; x++ {
 // append can skip make statement   
 pixels[y] = append(pixels[y],uint8(x*y)) 
  
     }
}

 return pixels
}

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

huangapple
  • 本文由 发表于 2012年7月21日 02:31:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/11585008.html
匿名

发表评论

匿名网友

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

确定