I have these types defined:
func init() {
gob.RegisterName("MyMessageHeader", MyMessageHeader{})
gob.RegisterName("OtherMsg", OtherMsg{})
}
//
// Messages
//
type MyMessageHeader struct {
MessageId InstanceIdType
OtherId uint64
}
type MyMessage interface {
Id() *MyMessageHeader
}
type otherStruct struct {
X uint8
Y uint8
}
type OtherMsg struct {
MyMessageHeader
OtherField *otherStruct
}
func (m *OtherMsg) Id() *MyMessageHeader {
return &m.MyMessageHeader
}
func MarshalMessage(m MyMessage) ([]byte, error) {
var buff bytes.Buffer
encoder := gob.NewEncoder(&buff)
if err := encoder.Encode(&m); err != nil {
return nil, errors.Newf("Could not marshal message: %v", err)
}
return buff.Bytes(), nil
}
func UnmarshalMessage(buf []byte) (MyMessage, error) {
var msg MyMessage
decoder := gob.NewDecoder(bytes.NewReader(buf))
if err := decoder.Decode(&msg); err != nil {
return nil, errors.Newf("Could not decode my message: %v", err)
}
return msg, nil
}
When I try to use UnmarshalMessage
I got the error gob: OtherMsg is not assignable to type MyMessage
.
I have no difference with the usage in the go examples for encoding and decoding an interface. What am I doing wrong?