我正在编写单元测试,我的目标是将数据从 JSON 反序列化为一个结构体,并将其与另一个模拟结构体进行比较。我使用 reflect.DeepEqual() 方法进行比较,但它返回 false。
我猜想这可能与后台中进行的类型转换有关,其中 map[string]interface{} 转换为 map[string]int,但这是我所能到达的极限。
输出结果如下:
我猜想这可能与后台中进行的类型转换有关,其中 map[string]interface{} 转换为 map[string]int,但这是我所能到达的极限。
type MyStruct struct {
Cache map[string]interface{} `json:"cache"`
}
var js = `{"cache":{"productsCount":28}}`
func main() {
var s1, s2 MyStruct
s1 = MyStruct{
Cache: map[string]interface{} {
"productsCount": 28,
},
}
s2 = MyStruct{}
err := json.Unmarshal([]byte(js), &s2)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("%#v\n", s1)
fmt.Printf("%#v\n", s2)
fmt.Println(reflect.DeepEqual(s1, s2))
}
输出结果如下:
main.MyStruct{Cache:map[string]interface {}{"productsCount":28}}
main.MyStruct{Cache:map[string]interface {}{"productsCount":28}}
false