I got this error by trying to load gob files which did not save() successfully, but I lacked sufficient error handling on save() to know it.
My save function is now a little insane, but I like it:
func SaveToDisk[T any](filepath string, data T) {
f, err := os.Create(filepath)
if err != nil {
log.Fatal(errors.Wrap(err, "os.Create()"))
}
defer func() {
err := f.Close()
if err != nil {
log.Fatal(errors.Wrap(err, "f.Close()"))
}
}()
writer := bufio.NewWriter(f)
dataEncoder := gob.NewEncoder(writer)
err = dataEncoder.Encode(data)
if err != nil {
log.Fatal(errors.Wrap(err, "dataEncoder.Encode()"))
}
err = writer.Flush() // Flush the data to the file
if err != nil {
log.Fatal(errors.Wrap(err, "writer.Flush()"))
}
}
Turns out I was missing the error dataEncoder.Encode(): gob: type not registered for interface: map[string]interface {}
during save().
gob
cannot seem to marshal interfaces without any help, but you can register interface types with it to remedy that. To fix my problem, all I had to do was:
func init() {
gob.Register(map[string]interface{}{})
}