I am pulling some data out of a database with Go, and one of the fields of that structure is a JSON field that can be one of several types. How can I cast the parsed json to the correct type in Go?
type DBStruct struct {
Type string `json:"type"`
Config interface{} `json:"config"` //ConfigA or ConfigB
}
type ConfigA struct {
Start string `json:"start"`
End string `json:"end"`
}
type ConfigB struct {
Title string `json:"title"`
}
func doSomethingWithStruct(s DBStruct) {
switch s.Type {
case "ConfigA":
config := GetConfigASomeHow(s.Config)
case "ConfigB":
config := GetConfigBSomeHow(s.Config)
}
}