drb56625 2017-10-18 11:50
浏览 163
已采纳

为什么不能在Go中将变量作为多维数组大小放置?

Recently, I've been interested in machine learning more specifically, machine learning with images, but to do that, I need to be able to process images. I want to have a more thorough understanding of how image processing libraries work along the way, so I have decided to make my own library for reading images that I can understand. However, I seem to have an issue when it comes to reading the SIZE of an image, as this error pops up when I try to compile:

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

This is my code:

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() {


}

I'm just beginning programming with Go, so I'm sorry if this question sounds nooby

  • 写回答

1条回答 默认 最新

  • duanma8207 2017-10-18 11:54
    关注

    Because Go is a statically typed language, which means types of variables need to be known at compile-time.

    Arrays in Go are fixed sizes: once you create an array in Go, you can't change its size later on. This is so to an extent that the length of an array is part of the array type (this means the types [2]int and [3]int are 2 distinct types).

    The value of a variable is generally not known at compile-time, so using that as the array length, the type would not be known at compile time, hence it is not allowed.

    Read related questions: How do I find the size of the array in go

    If you don't know the size at compile time, use slices instead of arrays (there are other reasons to use slices too).

    For example this code:

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

    Can be transformed to create and use a slice like this:

    func addImage(path string, image string, Height int, Width int){
        var Size int
        Size = Width * Height
        var Pix = make([][3]int, Size)
        // use Pix
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?