Google Go错误 – “无法创建类型”

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

Google Go error - "cannot make type"

问题

在我的Go代码中,我想创建一个自定义数据类型的数组。我调用

Blocks=make(*BlockData, len(blocks))

然后我得到错误:

无法创建类型*BlockData

我的BlockData类包含了uint64、int64、float32、string、[]byte、[]string和[]*TransactionData等字段类型。最后一个是指向另一个自定义类的指针数组。

我应该怎么做来修复这个错误?

英文:

In my Go code I want to make an array of custom data type. I call

Blocks=make(*BlockData, len(blocks))

and I get error:

cannot make type *BlockData

my class BlockData contains such field types as uint64, int64, float32, string, []byte, []string and []*TransactionData. The last one is an array of pointers to another custom class of mine.

What should I do to fix this error?

答案1

得分: 14

make() 用于创建切片、映射和通道。当创建切片时,类型名称前必须有 []

使用以下代码创建指向 BlockData 的指针切片。

Blocks = make([]*BlockData, len(blocks))

Go 语言规范中了解更多信息。

英文:

make() is used to create slices, maps and channels. The type name must have [] before it when making a slice.

Use this to make a slice of pointers to BlockData.

Blocks = make([]*BlockData, len(blocks))

Read more in the Go language specification.

答案2

得分: 1

Making slices, maps and channels

例如,

package main

import "fmt"

type BlockData struct{}

func main() {
    blocks := 4
    Blocks := make([]*BlockData, blocks)
    fmt.Println(len(Blocks), Blocks)
}

输出:

4 [<nil> <nil> <nil> <nil>]
英文:

Making slices, maps and channels

For example,

package main

import &quot;fmt&quot;

type BlockData struct{}

func main() {
	blocks := 4
	Blocks := make([]*BlockData, blocks)
	fmt.Println(len(Blocks), Blocks)
}

Output:

4 [&lt;nil&gt; &lt;nil&gt; &lt;nil&gt; &lt;nil&gt;]

huangapple
  • 本文由 发表于 2011年12月9日 16:39:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/8442989.html
匿名

发表评论

匿名网友

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

确定