假如我向其他人发起了一个rpc请求,对方返回结果如下:
{"errno":0,"errmsg":"success","data":{"12345678":{"total":{"count":170000},"request_id":"111111111","ie":"utf-8"}
那么对于这样的返回结果,第一种方式可以使用结构体来接(推荐),定义如下结构体:
type ResBody struct {
Data map[string]RestData `json:"data"`
ErrMsg string `json:"errmsg"`
ErrNo int `json:"errno"`
RequestID string `json:"request_id"`
Ie string `json:"ie"`
}
type ResData struct {
Total ResTotal `json:"total"`
}
type ResTotal struct {
Count int64 `json:"count"`
}
第二种方法,可以使用map来接,但是这种方式接到后,需要不断进行类型断言(用is-ok模式):
type ResBody struct {
Data map[string]interface{} `json:"data"`
ErrMsg string `json:"errmsg"`
ErrNo int `json:"errno"`
RequestID string `json:"request_id"`
Ie string `json:"ie"`
}