Gob error when decoding array of structs: decoding into local type but received remote type

35 Views Asked by At

When decoding an array of structs encoded by gob I am getting the following error: Error decoding inodes: gob: decoding into local type *[]filesystem.Inode, received remote type [120] = struct { IsValid bool; IsDirectory bool; Datablocks [4]int; Filecreated Time; Filemodified Time; Inodenumber int; } here is my Inode struct

type Inode struct {
    IsValid      bool
    IsDirectory  bool
    Datablocks   [4]int
    Filecreated  time.Time
    Filemodified time.Time
    Inodenumber  int
}

and here is the code doing encoding and decoding

func InitializeDisk() {
    var encoder bytes.Buffer
    enc := gob.NewEncoder(&encoder)
    var inode Inode
    for i := range Inodes {
        inode.Datablocks = [4]int{0, 0, 0, 0}
        inode.IsDirectory = false
        inode.IsValid = false
        inode.Filecreated = time.Now()
        inode.Filemodified = time.Now()
        inode.Inodenumber = i
        Inodes[i] = inode
    }
    for i := range BlockBitmap {
        BlockBitmap[i] = false
    }
    for i := range InodeBitmap {
        InodeBitmap[i] = false
    }

    var superblock SuperBlock
    superblock.Inodeoffset = 3
    superblock.Blockbitmapoffset = 2
    superblock.Inodebitmapoffset = 1

    err := enc.Encode(superblock)
    if err != nil {
        log.Fatal(err)
    }
    for i := range encoder.Bytes() {
        VirtualDisk[0][i] = encoder.Bytes()[i]
    }
    encoder.Reset()

    bitmapBytesInode := boolsToBytes(InodeBitmap[:])
    EndInodeBitmap = len(bitmapBytesInode)
    for i := range bitmapBytesInode {
        VirtualDisk[1][i] = bitmapBytesInode[i]
    }

    bitmapBytesBlocks := boolsToBytes(BlockBitmap[:])
    EndBlockBitmap = len(bitmapBytesBlocks)
    for i := range bitmapBytesBlocks {
        VirtualDisk[2][i] = bitmapBytesBlocks[i]
    }

    var buf bytes.Buffer
    gob.NewEncoder(&buf).Encode(Inodes)
    data := buf.Bytes()
    fmt.Println(len(data))
    blockSize := Blocksize
    inodeOffset := int(superblock.Inodeoffset)
    for i := 0; i < len(data); i += blockSize {
        blockIndex := inodeOffset + i/blockSize
        end := i + blockSize
        if end > len(data) {
            end = len(data)
        }
        copy(VirtualDisk[blockIndex][:], data[i:end])
    }
    var testInodes []Inode
    decoder := gob.NewDecoder(bytes.NewReader(data))
    err = decoder.Decode(&testInodes)
    fmt.Println(err)
    fmt.Println(testInodes)
} //end of initialize disk

The relevant part is toward the end beginning with var buf byte.Buffer. I am calling InitializeDisk from a main.go file one directory up from this file. InitializeDisk is part of a subpackage called filesystem

1

There are 1 best solutions below

0
Thomas Hutton On

var testInodes []Inode had to be in the form var testInodes [120]Inode as my global variable Inode array that was initially encoded is initialized to 120