料青山看我应如是
在您的 Go Playground 示例中,您尝试对接口进行编码,而接口没有具体的实现。如果您从A结构中删除接口,那应该可以。像下面这样:package mainimport "fmt"import "encoding/gob"import "bytes"type testInterface interface{}type A struct { Name string Interface *B // note this change here}type B struct { Value string}func main() { var err error test := &A { Name: "wut", Interface: &B{Value: "BVALUE"}, } buf := bytes.NewBuffer([]byte{}) enc := gob.NewEncoder(buf) dec := gob.NewDecoder(buf) // added error checking as per Mark's comment err = enc.Encode(test) if err != nil { panic(err.Error()) } result := &A{} err := dec.Decode(result) fmt.Printf("%+v\n", result) fmt.Println("Error is:", err) fmt.Println("Hello, playground")}此外,作为旁注,您将看到类似以下的某种输出:&{Name:wut Interface:0x1040a5a0}因为A正在引用对B结构的引用。进一步清理:type A struct{ Name string Interface B // no longer a pointer}func main() { // ... test := &A{Name: "wut", Interface: B{Value: "BVALUE"}} // ...}