expected := model.Item{
Url: "https://",
Id: "1470740797",
Profile: model.Profile{
NickName: "乌雲下的黑玫瑰",
Marriage: "离异",
Age: 35,
StarSign: "魔羯座(12.22-01.19)",
Height: 155,
Place: "工作地:阿坝汶川",
},
}
// Item类型
type Item struct {
Url string
Id string
Profile interface{}
}
var actual model.Item
json.Unmarshal(*resp.Source, &actual)
actual和expected无法比较,因为actual.Profile的类型是interface{}而expected.Profile是Profile类型。
解决;
func FromJsonObj(o interface{}) (profile Profile, err error){
bytes, err := json.Marshal(o)
if err != nil {
return Profile{}, err
}
err = json.Unmarshal(bytes, &profile)
return profile, err
}
actualProfile := FromJsonObj(actual.Profile)
actual.Profile = actualProfile
把actual.Profile的类型是转换成Profile类型