Help getting troubles with gob.Decode / Encode

Hey all this is my very firist time in the forum.
So i want to write some structs in a binary file, i was searching and i found this two functions to do it.

func writeGob(path string, object interface{}, offset int64) error {
	file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0777)

	if err != nil {
		PrintError("Error intentando abrir el archivo")
		return errors.New("Error intentado abrir el archivo")
	}

	file.Seek(offset, 0)
	encoder := gob.NewEncoder(file)
	encoder.Encode(object)
	file.Close()
	return err

}

func readGob(path string, offset int64, object interface{}) error {
	file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 666)
	if err != nil {
		PrintError("Error intentando abrir el archivo: " + err.Error())
		return errors.New("Error intentando abrir el archivo")
	}

	file.Seek(offset, 0)
	decoder := gob.NewDecoder(file)

	err = decoder.Decode(object)

	file.Close()
	return err // must be nil
}

this are the two structs that im using

type Ebr struct {
	PartStatus uint8
	PartFit    uint8
	PartStart  int64
	PartSize   int64
	PartName   [16]uint8
	PartNext   int64
}

type Mbr struct {
	MbrTamanio       int64
	MbrFechaCreacion [20]uint8
	MbrDiskSignature int64
	DiskFit          uint8
	MbrPartition     [4]Partition
}

for getting you and idea this is a tinny ext2 implementation for a college work

everything was going fine, until i tried to read an ebr and i am getting this error

gob: Duplicate types received

This is the line code where the error occurs

var ebr = new(Ebr)
err := readGob(c.Args["path"].(string), ext.PartStart, ebr)

i search that error on google but i dont found any help. so if u can help me u will make me a very happy men

You are likely reading from the wrong offset and the gob internals are misinterpreting what they are trying to decode.

Thanks for you reply. I was debugging and de offset seems to be the same. i made some test writing the ebr and next reading it and everything its fine. But if i try to read it from another part of the code, the error appears.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.