go - 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)
我希望差异为零
解决方案
由于结构是从 json 解组的,因此 json 将所有数字视为浮点数
推荐阅读
- performance - 各种 Power BI 数据源的(相对)性能如何?
- parsing - Haskell:如何将语义空白集成到解析器中?
- oracle - 过程中的相同查询需要不同的时间
- typescript - 反应导航类型检查
- postgresql - 如何在 pgadmin-4 中创建程序?
- python - 在 python 的 azure 函数中安装 google apis 时出错
- reactjs - Index.js 文件中的文件夹结构,未找到模块错误
- javascript - 使用 Passport 在单击“使用 Google 登录”按钮时重定向
- eclipse - 潜在的资源泄漏:'
' 不能用 SpringApplication.run(...) 关闭 - awk - 正则表达式问题的 awk 打印结果