1 回答

TA贡献1788条经验 获得超4个赞
你正在吞下一个error返回的Encoder.Encode():
err := gob.NewEncoder(bys).Encode(map[string]interface{}{
"v": (*T)(nil),
})
if err != nil {
fmt.Println(err)
}
输出:
gob: gob: cannot encode nil pointer of type *main.T inside interface
这是由未导出的方法生成的Encoder.encodeInterface()。引用encode.go未导出的方法Encoder.encodeInterface():
// Gobs can encode nil interface values but not typed interface
// values holding nil pointers, since nil pointers point to no value.
elem := iv.Elem()
if elem.Kind() == reflect.Ptr && elem.IsNil() {
errorf("gob: cannot encode nil pointer of type %s inside interface", iv.Elem().Type())
}
因此,您Encoder.Encode()失败了,它不会向其输出(即bys缓冲区)写入任何内容,因此尝试从中读取(解码)任何内容都会导致 EOF。
但是为什么你不能编码一个interface{}持有nil指针的值呢?引用包文档encoding/gob:
指针不被传输,但它们指向的东西被传输;也就是说,这些值是扁平的。
您interface{}包含一个指针类型的值,但该指针是nil,它不指向任何内容,它不能被展平。
这是 github 上的一个相关问题:encoding/gob: panic on encoding nil pointer #3704
拉斯:
gob 不知道指针是什么:一切都变平了。将 nil 指针放在 interface{} 值中会创建一个非零值(它不是 nil 接口),gob 无法发送(它不能表示“nil 指针”)。
罗布·派克:
正确的。只有具体值本身是可传输的,才能传输接口值。至少现在,这相当于说不能发送持有类型化 nil 指针的接口。
- 1 回答
- 0 关注
- 233 浏览
添加回答
举报