心有法竹
如果您知道要使用的类型,则可以将它们放入数组列表中以实现一种多态实例化:package mainimport ( "fmt" "encoding/json")type Hans struct{ Miau string }type Keule struct { Wuff string}func (K Keule)ppp() { fmt.Printf(K.Wuff)}func (K Hans)ppp() { fmt.Printf(K.Miau)}func (K Keule)TypeInfo() int { return 0}func (K Hans)TypeInfo() int { return 1}type Mega interface { ppp() TypeInfo() int}var j_a = `{ "Kein_Alter": "nix", "Miau": "lala", "class": 0}`var j_b = `{ "Alter": "nix", "Wuff": "lolo", "Class": 1}`type Class struct { Class int}func (K *Class)ppp() { fmt.Printf("%d", K.Class)}func Unmarshal_K(b []byte) (Mega, error) { var k Keule err := json.Unmarshal([]byte(j_a), &k) return k, err}func Unmarshal_H(b []byte) (Mega, error) { var k Hans err := json.Unmarshal([]byte(j_a), &k) return k, err}var UList = []func(b []byte) (Mega, error) {Unmarshal_H, Unmarshal_K}func main() { var mv Class err := json.Unmarshal([]byte(j_a), &mv) if err != nil { panic(err) } hiho, err := UList[mv.Class]([]byte(j_a)) if err != nil { panic(err) } hiho.ppp()}