首页 > 解决方案 > go-cmp 在比较 2 个结构时将空接口值视为 int 和 flat64

问题描述

所以我正在使用 go-cmp github.com/google/go-cmp/cmp。比较2个结构。两个结构都是相同的类型。比较结果时,我看到以下差异。

我正在使用 go v1.12

type Sample struct{
   Field1 map[string]interface
}
var a Sample
var b Sample

cmp.Diff(a,b)


// Somewhere in code I do this
a.Field1["sample"] = 1 // this is optional.

因此,如果我比较 a 和 b,我会看到它解释与 int(0) 和 float64(0) 相同的字段的差异

-: int(0)
+: float64(0)

我希望差异为零

标签: goreflection

解决方案


由于结构是从 json 解组的,因此 json 将所有数字视为浮点数


推荐阅读