无法在Go语言中将字符串类型“a”用作数组元素的类型。

huangapple go评论84阅读模式
英文:

Cannot use "a" (type string) as type in array element in go

问题

我对Golang还不太熟悉,我在向数组中添加项目方面遇到了问题。

我使用这个链接作为参考golang-book

我有这个结构体:

package models

type FileMD struct {
	fileName string
	fileSize int
}

我尝试了两种方法,但都失败了。

fileList := [...]models.FileMD{"a", 1: "b", 2}
var fileList [...]models.FileMD
fileList[0] = "a", 1

正确的方法是什么?

英文:

I'm pretty new to Golang and I have an issue with adding items to Array.

I use this link as a reference golang-book.

I have this struct:

package models

type FileMD struct {
	fileName string
	fileSize int
}

I tried to do it both ways but I failed.

fileList := [...]models.FileMD{"a", 1: "b", 2}
var fileList [...]models.FileMD
fileList[0] = "a", 1

What is the correct way?

答案1

得分: 4

我不确定,但我认为你正在寻找的是:

fileList[0] = FileMD{"a", 1}

或者可能是:

fileList := []FileMD{{"a", 1}, {"b", 2}}
英文:

I am not sure but I think you are looking for:

fileList[0] = FileMD{"a", 1}

Or maybe:

fileList := []FileMD{{"a", 1}, {"b", 2}}

答案2

得分: 4

首先,你需要决定你想要一个数组(array)还是一个切片(slice)。

一旦你决定了,你基本上有两个选项:

(在Go Playground上尝试完整的应用程序。)

1)使用字面量:###

使用复合字面量(数组字面量或切片字面量)初始化你的数组/切片,然后你就完成了。

数组:

fileArr10 := [2]FileMD{FileMD{"first", 1}, FileMD{"second", 2}}
// 或者简写:
fileArr11 := [2]FileMD{{"first", 1}, {"second", 2}}
// 自动计算长度:
fileArr20 := [...]FileMD{FileMD{"first", 1}, FileMD{"second", 2}}
// 或者简写:
fileArr21 := [...]FileMD{{"first", 1}, {"second", 2}}

切片(注意数组字面量和切片字面量之间唯一的区别是没有指定长度):

fileSl10 := []FileMD{FileMD{"first", 1}, FileMD{"second", 2}}
// 或者简写:
fileSl11 := []FileMD{{"first", 1}, {"second", 2}}

2)创建新的、初始化的数组/切片并填充它:###

创建一个新的、初始化的数组/切片,并通过给其元素赋值来填充它。

数组:

fileArr := [2]FileMD{}
fileArr[0] = FileMD{"first", 1}
fileArr[1] = FileMD{"second", 2}

切片:(你可以使用内置函数make创建切片)

fileSl := make([]FileMD, 2)
fileSl[0] = FileMD{"first", 1}
fileSl[1] = FileMD{"second", 2}

最后注意

你的结构体FileMD是一个包的一部分,它包含未导出的字段(字段名以小写字母开头)。

未导出的字段无法从包的外部访问。这也意味着当你创建这样的结构体的值时,无法指定未导出字段的初始值。

因此,如果你尝试从models包的外部创建一个FileMD的值,你只能创建零值的FileMD,如FileMD{}。将你的字段导出,然后你就可以为文件名和大小指定初始值:

type FileMD struct {
	FileName string
	FileSize int
}

// 现在,在`models`包的外部,FileMD{"first", 1}是有效的
英文:

First you need to decide if you want an array or a slice.

Once you decided, you have basically 2 options:

(Try the complete application on the Go Playground.)

1) With a literal:

Initialize your array/slice with a Composite literal (either an array or slice literal) and you're done.

Arrays:

fileArr10 := [2]FileMD{FileMD{"first", 1}, FileMD{"second", 2}}
// Or short:
fileArr11 := [2]FileMD{{"first", 1}, {"second", 2}}
// With length auto-computed:
fileArr20 := [...]FileMD{FileMD{"first", 1}, FileMD{"second", 2}}
// Or short:
fileArr21 := [...]FileMD{{"first", 1}, {"second", 2}}

Slices (note the only difference between array and slice literals is that length is not specified):

fileSl10 := []FileMD{FileMD{"first", 1}, FileMD{"second", 2}}
// Or short:
fileSl11 := []FileMD{{"first", 1}, {"second", 2}}

2) New, initialized array/slice and filling it:

Create a new, initialized array/slice and fill it by assiging values to its elements.

Array:

fileArr := [2]FileMD{}
fileArr[0] = FileMD{"first", 1}
fileArr[1] = FileMD{"second", 2}

Slice: (you can create slices with the built-in function make)

fileSl := make([]FileMD, 2)
fileSl[0] = FileMD{"first", 1}
fileSl[1] = FileMD{"second", 2}

Final Note

Your struct FileMD is part of a package and it contains unexported fields (fields whose name start with lowercased letters).

Unexported fields are not accessible from outside of the package. This also means that when you create a value of such struct, you cannot specify the initial values of unexported fields.

So if you try to create a value of FileMD from outside of package models, you can only create zero-valued FileMDs like this: FileMD{}. Make your fields exported and then you can specify the initial values for the file name and size:

type FileMD struct {
	FileName string
	FileSize int
}

// Now FileMD{"first", 1} is valid outside of package models

huangapple
  • 本文由 发表于 2015年2月25日 23:41:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/28723199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定