我相信这是 Gob 序列化的合法用例。但是由于没有导出字段而enc.Encode
返回错误。Something
请注意,我不是Something
直接序列化,而是仅Composed
包含导出的字段。
我发现的唯一解决方法是将Dummy
(导出的)值添加到Something
. 这很丑陋。有没有更优雅的解决方案?
https://play.golang.org/p/0pL6BfBb78m
package main
import (
"bytes"
"encoding/gob"
)
type Something struct {
temp int
}
func (*Something) DoSomething() {}
type Composed struct {
Something
DataToSerialize int
}
func main() {
enc := gob.NewEncoder(&bytes.Buffer{})
err := enc.Encode(Composed{})
if err != nil {
panic(err)
}
}