gomethodsinterfacegob

Gob decode cannot decode interface after register type


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?


Solution

  • The error message says:

    gob: OtherMsg is not assignable to type MyMessage

    Which is true. MyMessage is an interface which requires implemener types to have a method:

    Id() *MyMessageHeader
    

    A value of type OtherMsg does not have such method in its method set, only a pointer to it, that is a value of type *OtherMsg (because OtherMsg.Id() has pointer receiver).

    To make it work, register a value of type *OtherMsg:

    gob.RegisterName("OtherMsg", &OtherMsg{})
    

    Or simply:

    gob.Register(&OtherMsg{})
    

    See a working example on the Go Playground:

    func init() {
        gob.Register(&OtherMsg{})
    }
    
    func main() {
        var m MyMessage = &OtherMsg{
            MyMessageHeader: MyMessageHeader{
                MessageId: 1,
                OtherId:   2,
            },
            OtherField: "othervalue",
        }
    
        data, err := MarshalMessage(m)
        if err != nil {
            panic(err)
        }
    
        m2, err := UnmarshalMessage(data)
        if err != nil {
            panic(err)
        }
        fmt.Printf("%+v\n", m2)
    }
    

    Output (try it on the Go Playground):

    &{MyMessageHeader:{MessageId:1 OtherId:2} OtherField:othervalue}