2012-07-20 2 views
4

나는 Go 언어 투어에서 엑서사이즈를 진행 중이며 알아낼 수없는 걸림돌을 쳤다. 나는 Exercise: Slices을하고 그리고 난이 오류가 무엇입니까 : 나는이 문제를 찾을 수없는 내 인생Go Tour, Excercise : Slices 인덱스 범위를 벗어남

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

:

여기
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 

내 코드입니다!

답변

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 (픽셀 [] [] 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)를 만들면 dint uint8s를 유지할 수 있지만 현재 비어있는 슬라이스를 만들지 않겠는가? – CaseyB

+0

내 대답보기. 'make ([uint8, dx)]는'make ([uint8, dx, dx)]와 동일하다. 즉, len() = dx와 cap() = dx는'make ([] uint8 , 0, dx)'즉, len() = 0이고 cap() = dx입니다. 두 경우 모두 기본 배열은 cap() = dx 0 값 요소입니다. 인덱스는 0 <= i peterSO

-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

변경 사항에 대한 설명이나 설명없이 코드 만 응답하거나 그다지 도움이되지 않는 이유는 무엇입니까? –

관련 문제