2012-07-20 27 views
4

我正在通過Go語言遊的練習中工作,並且遇到了一個我無法弄清楚的障礙。我在做Exercise: Slices和我收到此錯誤:Go Tour,Excercise:切片索引超出範圍

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 

這裏是我的代碼:

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

對於我的生活,我不能找到問題!

回答

6

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必須是一個整數值和0 < = Y < LEN(像素[] UINT8)。 x必須是一個整數值並且0 < = x < len(pixel [] [] uint8)。例如,

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

所以使([] UINT8,DX)使得在它DX零分得一杯羹?如果這種情況與容量不同,情況如何?我的意思是如果我確定([] uint8,0,dx)不會創建一個可以容納dx uint8s的片段,但我們目前是空的? – CaseyB 2012-07-20 20:02:32

+0

看到我的答案。 'make([] uint8,dx)'相當於'make([] uint8,dx,dx)'即len()= dx和cap()= dx,它與'make([] uint8 ,0,dx)'即len()= 0,cap()= dx。在這兩種情況下,底層數組都是cap()= dx零值元素。索引必須爲0 <= i peterSO 2012-07-20 20:34:20

-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] = make([]uint8, 0, dx) 

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

    } 
} 

return pixels 
} 

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

代碼只回答沒有任何描述或解釋什麼樣的改變或爲什麼沒有什麼幫助。 – 2015-04-11 11:15:28