2017-10-18 7 views
0

최근에는 이미지로 기계 학습을 좀 더 구체적으로 배우는 기계에 관심이 있었지만 이미지를 처리 ​​할 수 ​​있어야합니다. 이미지 처리 라이브러리가 어떻게 작동하는지 더 철저하게 이해하고 싶습니다. 그래서 이해할 수있는 이미지를 읽을 수있는 자체 라이브러리를 만들기로 결정했습니다.Go에 변수를 다차원 배열 크기로 넣을 수없는 이유는 무엇입니까?

package main 

import (
// "fmt" 
// "os" 
) 

// This function reads a dimension of an image you would use it like readImageDimension("IMAGENAME.PNG", "HEIGHT") 

func readImageDimension(path string, which string) int{ 

var dimensionyes int 

if(which == "" || which == " "){ 
    panic ("You have not entered which dimension you want to have.") 
} else if (which == "Height" || which == "HEIGHT" || which == "height" || which == "h" || which =="H"){ 
    //TODO: Insert code for reading the Height of the image 

    return dimensionyes 

} else if (which == "Width" || which == "WIDTH" || which == "width" || which == "w" || which =="W"){ 
    //TODO: Insert code for reading the Width of the image 

    return dimensionyes 

} else { 
    panic("Dimension type not recognized.") 
    } 
} 

func addImage(path string, image string, Height int, Width int){ 
    var Size int 
    Size = Width * Height 
    var Pix [Size][3]int 
} 

func main() { 


} 
:

./imageProcessing.go:33:11: non-constant array bound Size 

이 내 코드입니다 : 그러나, 나는이 이미지의 SIZE 읽기에 올 때 내가 컴파일 할 때이 오류가 팝업 등의 문제를 갖고있는 것 같다

나는 Go로 프로그래밍을 막 시작 했으므로이 문제가 nooby로 들리면 유감이다.

답변

5

Go는 정적 유형의 언어이므로 컴파일 할 때 변수 유형을 알아야하기 때문에.

Arrays은 고정 크기입니다. 일단 이동에 배열을 만들면 나중에 크기를 변경할 수 없습니다. 배열의 길이가 배열 유형의 일부인 정도입니다 (즉 [2]int[3]int 유형이 2 개의 고유 한 유형 임).

일반적으로 변수 값은 컴파일 타임에 알 수 없기 때문에이를 배열 길이로 사용하면 컴파일 타임에 유형을 알 수 없으므로 허용되지 않습니다.

보기 관련 질문 : 당신은 컴파일 타임에 크기를 알 수없는 경우 How do I find the size of the array in go

대신 배열의 slices을 사용 (너무 조각을 사용하는 다른 이유가있다).

예를 들어,이 코드 :

func addImage(path string, image string, Height int, Width int){ 
    var Size int 
    Size = Width * Height 
    var Pix = make([][3]int, Size) 
    // use Pix 
} 
:

func addImage(path string, image string, Height int, Width int){ 
    var Size int 
    Size = Width * Height 
    var Pix [Size][3]int 
    // use Pix 
} 

이 같은 조각을 만들고 사용하는 변형 될 수있다