为什么不能在Go中将变量作为多维数组大小放置? [英] Why can't you put a variable as a multidimensional array size in Go?

查看:290
本文介绍了为什么不能在Go中将变量作为多维数组大小放置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我一直对机器学习感兴趣,更具体地说,是对图像的机器学习,但是要做到这一点,我需要能够处理图像。我想更全面地了解图像处理库在此过程中的工作方式,因此,我决定创建自己的库来读取可以理解的图像。但是,在读取图像的 SIZE 时似乎遇到了问题,因为在尝试编译时会弹出此错误:

  ./imageProcessing.go:33:11:非恒定数组绑定大小

这是我的代码:

  package main 

import(
// fmt
// os


//此函数读取要使用的图像尺寸,如readImageDimension( IMAGENAME.PNG , HEIGHT)

func readImageDimension(路径字符串,该字符串)int {

var Dimensionsyes int

if(which == ||其中==){
恐慌(您尚未输入要拥有的尺寸。)
}否则if(其中==高度 ||其中==高度 ||其中==高度 ||其中== h ||其中== H){
// TODO:插入用于读取图像高度的代码

returnDimensionyes

}否则,如果(其中==宽度 ||其中==宽度 ||其中== 宽度 ||其中== w ||其中== W){
// TODO:插入代码以读取图像的宽度

返回尺寸是

}否则{
panic(无法识别尺寸类型。)
}
}

func addImage(路径字符串,图像字符串,Height int,Width int){
var Size int
大小=宽度*高度
var Pix [Size] [3] int
}

func main(){


}

我才刚开始使用Go编程,所以很抱歉这个问题听起来很笨

解决方案

因为Go是一种静态类型的语言,这意味着需要在编译时知道变量的类型。 / p>

= a href = https://golang.org/ref/spec#Array_types rel = noreferrer>数组是固定大小的:在Go中创建数组后,以后就无法更改其大小。在某种程度上来说,数组的长度是数组类型的一部分(这意味着类型 [2] int [3] int 是2个不同的类型。)



在编译时通常不知道变量的值,因此将其用作数组长度,类型将在编译时未知,因此是不允许的。



阅读相关问题:如何在行进中找到数组的大小



如果在编译时不知道大小,请使用切片而不是数组(也有使用切片的其他原因)。



例如以下代码:

  func addImage(路径字符串,图像字符串,Height int,Width int){
var Size int
Size = Width * Height
var Pix [大小] [3] int
//使用Pix
}

可以转换为创建和使用像这样的切片:

  func addImage(路径字符串,图像字符串,Height int,Width int){
var Size int
Size = Width *高度
var Pix = make([[3] int,Size)
//使用Pix
}


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

解决方案

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
}

这篇关于为什么不能在Go中将变量作为多维数组大小放置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆