小编yal*_*gko的帖子

gob 恐慌解码接口

我有一个带有未导出字段的结构,应该对其进行 gob 编码和解码。

说:

type A struct {
    s int
}
func (a *A) Inc() {
    a.s++
}
Run Code Online (Sandbox Code Playgroud)

显然在这种情况下我需要实现gob.GobEncodergob.GobDecoder接口。如果我直接使用该结构,一切都会正常

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

但我还需要一个实现相同逻辑并且可序列化的接口:

type Incer interface {
    gob.GobEncoder
    gob.GobDecoder
    Inc()
}
Run Code Online (Sandbox Code Playgroud)

完整代码: https: //play.golang.org/p/Zig2mPrnrq

突然它恐慌了:

panic: interface conversion: interface is nil, not gob.GobDecoder [recovered]
    panic: interface conversion: interface is nil, not gob.GobDecoder
Run Code Online (Sandbox Code Playgroud)

但如果我评论 gob 接口,一切都会变得很好。

我错过了什么重要的事情吗?因为所描述的行为对我来说似乎很奇怪

encoding go gob

6
推荐指数
1
解决办法
1135
查看次数

标签 统计

encoding ×1

go ×1

gob ×1