Gob panics decode interface

I have a structure with unfulfilled fields that need to be encoded and decoded.

Say:

type A struct {
    s int
}
func (a *A) Inc() {
    a.s++
}

Obviously, in this case I need to implement the gob.GobEncoderand interfaces gob.GobDecoder. And , if I use the structure directly, everything works fine :

https://play.golang.org/p/dm3HwaI8eU

But I also need an interface that implements the same logic and serializable:

type Incer interface {
    gob.GobEncoder
    gob.GobDecoder
    Inc()
}

Full code: https://play.golang.org/p/Zig2mPrnrq

And then it panics:

panic: interface conversion: interface is nil, not gob.GobDecoder [recovered]
    panic: interface conversion: interface is nil, not gob.GobDecoder

But if I comment on gob interfaces, everything will be fine again.

Did I miss something important? Because the described behavior seems to me rather strange.

+4
source share
2

, Incer , encoding/gob: gob.GobDecoder.

Incer Inc(), , gob , , , ( ) , :

type Incer interface {
    Inc()
}

, .

Incer gob.GobEncoder gob.GobDecoder, , , /. , , GobDecode() , .

nil Decoder.Decode(), , , , . , , .

gob.GobEncoder gob.GobDecoder Incer . , , , , , "" Incer. Incer: gob.GobEncoder gob.GobDecoder - , encoding.BinaryMarshaler encoding.BinaryUnmarshaler, encoding/gob.

+2

, "" , .

var v Incer

, . , main.A struct, (* A).GobEncode() (* A).GobDecode(), :

var v Incer
v = &A{}

: https://play.golang.org/p/WUNRKGTVIS

, gob

+1

Source: https://habr.com/ru/post/1674479/


All Articles