(2)interface
比如JSON有以下两种类型:
{"Type":"sound","Msg":{"Description":"dynamite","Authority":"the Bruce Dickinson"}}
{"Type":"cowbell","Msg":{"More":true}}
Msg 具体什么类型实现无法判断, Msg being a map[string]interface{} :
type Envelope struct {
Type string
Msg interface{}
}
var env Envelope
if err := json.Unmarshal([]byte(input), &env); err != nil {
log.Fatal(err)
}
// for the love of Gopher DO NOT DO THIS
var desc string = env.Msg.(map[string]interface{})["description"].(string)
fmt.Println(desc)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。









