Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 538
ThePiachu Avatar asked Dec 09 '11 08:12

ThePiachu


2 Answers

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.

like image 154
RCE Avatar answered Oct 03 '22 13:10

RCE


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>]
like image 31
peterSO Avatar answered Oct 03 '22 14:10

peterSO