Problem with gob and interfaces

66 views
Skip to first unread message

Christine

unread,
Dec 14, 2010, 8:59:54 AM12/14/10
to golang-nuts
I am encoding a struct with gob.
The struct contains a slice of an interface.
When I decode with gob, I get an error message:
Error with Decode(): interface conversion: main.TestStruct1 is not
main.Iface: missing method IsOK

Version of go used: 900cfd2def17 tip

Any help would be appreciated.


Here is the code:
package main

import (
"fmt"
"gob"
"bytes"
)

type Iface interface {
IsOK() bool
}

type msg struct {
ifaces []Iface
}

type TestStruct1 struct{}
func (this *TestStruct1) IsOK() bool {
return true
}

func main(){
gob.Register(&TestStruct1{})

readerwriter := bytes.NewBuffer(nil)

writeVal := &msg{[]Iface{&TestStruct1{}}}
encoder := gob.NewEncoder(readerwriter)
err := encoder.Encode(&writeVal)
if (err != nil) {
fmt.Println("Error with Encode():", err.String())
}

var readVal msg
decoder := gob.NewDecoder(readerwriter)
err = decoder.Decode(&readVal)
if (err != nil) {
fmt.Println("Error with Decode():", err.String())
}

fmt.Println("encoder.Decoded: %v", readVal)
}

Andrew Gerrand

unread,
Dec 14, 2010, 5:29:15 PM12/14/10
to Christine, golang-nuts, Rob Pike
Looks like a bug. We're looking into it.

Andrew Gerrand

unread,
Dec 14, 2010, 7:05:15 PM12/14/10
to Christine, golang-nuts, Rob Pike
A fix for this issue has been submitted, and will be in the next
release. (If you can't wait that long, you could hgupdate to tip.)

Christine

unread,
Dec 15, 2010, 4:57:29 AM12/15/10
to golang-nuts
Thanks
Reply all
Reply to author
Forward
0 new messages