go - 使用 go-cmp 正确比较和查找具有导出成员的两个结构之间的差异
问题描述
我正在编写一个 Go 应用程序,我想为它创建一个测试,在该测试中,我从数据库中查询一些内容,将其插入到一个结构中,并将该结构值与我相同类型的静态结构进行比较有,如果它们匹配,则测试成功,如果不匹配,我想显示差异。所以我正在尝试使用go-cmp
包。
一般来说,我收到此错误:
panic: cannot handle unexported field at {main.fooTest}.F1.Int.neg:
"math/big".Int
consider using a custom Comparer; if you control the implementation of type, you can also consider using an Exporter, AllowUnexported, or cmpopts.IgnoreUnexported [recovered]
我得到这是因为pgtype.Numeric
我的结构中有
type fooTest struct {
I1 int
I2 *int
S1 string
S2 *string
F1 pgtype.Numeric
F2 *pgtype.Numeric
Ff1 float64
Ff2 *float64
Ia1 []int
Ia2 []*int
Ia3 *[]int
Sa1 []string
Sa2 []*string
Sa3 *[]string
Fa1 pgtype.Float8Array
Fa2 *pgtype.Float8Array
Faf1 []float64
Faf2 []*float64
Faf3 *[]float64
}
so I tried to test with `AllowUnnexported` but the `cmp.Equal` line still fails with that error.
if !cmp.Equal(fooFoo, bar,cmp.AllowUnexported(fooTest{})) {
t.Errorf("failed test: %v",cmp.Diff(fooFoo, bar))
}
任何想法如何解决这个问题?
解决方案
你有没有尝试过
cmp.Equal(fooFoo, bar, cmp.AllowUnexported(pgtype.Numeric.Int{}))
的文档AllowUnexported
没有提到它递归地将其应用于结构中的所有值。
推荐阅读
- powershell - PS脚本在IE中为URL添加书签
- mongodb - 无法从 Power BI 连接到 mongo db 中的数据库
- nginx - 在启用 Proxy_Protocol 的情况下将 NGINX 流量代理到辅助代理
- r - R - 在“主键列”上将行从数据帧附加到另一个不重复
- reactjs - 是否可以扩展 react-docgen 以选择组件上的自定义属性?
- java - Jaxb - 在包含期间使用 rootElement 生成类
- python - 使用 matplotlib 显示多个 y 轴脊时减小一个子图的宽度
- c# - 连接远程数据库时如何诊断和修复TaskCanceledException?
- r - R - 包 marmap - read.bathy - 深度值 NA
- javascript - 如何使用 PHP 和 AJAX 删除/编辑 sql 条目?