您已经传递了
json一个指向抽象接口的指针。您应该简单地将指针
Ping作为 抽象接口传递:
func bad() { var ping interface{} = &Ping{} // <<<< this deserialize([]byte(`{"id":42}`), ping) // << and this fmt.Println("DONE:", ping) // It's a simple map now, not a Ping. Why?}但是,如您所说,如果您没有指针可以转换为an
interface{},则可以使用reflect创建一个新的指针,将其反序列化,然后将值复制回去:func bad() { var ping interface{} = Ping{} nptr := reflect.New(reflect.TypeOf(ping)) deserialize([]byte(`{"id":42}`), nptr.Interface()) ping = nptr.Interface() fmt.Println("DONE:", ping) // It's a simple map now, not a Ping. Why?}


