ThePiachu
ThePiachu

Reputation: 9185

Google Go error - "cannot make type"

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?

Upvotes: 11

Views: 9505

Answers (2)

RCE
RCE

Reputation: 1721

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.

Upvotes: 16

peterSO
peterSO

Reputation: 166895

Making slices, maps and channels

For example,

package main

import "fmt"

type BlockData struct{}

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

Output:

4 [<nil> <nil> <nil> <nil>]

Upvotes: 2

Related Questions