Golang: Unmarshalling JSON with multiple types

Viewed 40
type A struct {
    NameA         string `json:"nameA"`
}

type B struct {
    NameB         string `json:"nameB"`
}

type MyData struct {
    DataType      string `json:dataType`
    InnerData  A or B  string  `json:InnerData`
}

The type of InnerData is depend on DataType. when DataType is A, InnerData type will be Struct A

How to get the right type, when I use json.Unmarshal ?

0 Answers
Related