9

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?

2 Answers 2

14

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.

1

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>]

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.