标签: go-cmp

使用 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]
Run Code Online (Sandbox Code Playgroud)

我得到这个是因为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 …
Run Code Online (Sandbox Code Playgroud)

struct go go-cmp

5
推荐指数
1
解决办法
4732
查看次数

cmp.Equal 给出紧急消息“无法处理 .. 处的未导出字段”

我有一个自定义结构,其中有一个未导出的字段。我希望能够将值与此结构的类型进行比较,但我收到如下恐慌消息:

panic: cannot handle unexported field at ...
consider using a custom Comparer; if you control the implementation of type, you can also consider using an Exporter, AllowUnexported, or cmpopts.IgnoreUnexported
Run Code Online (Sandbox Code Playgroud)

示例代码:

panic: cannot handle unexported field at ...
consider using a custom Comparer; if you control the implementation of type, you can also consider using an Exporter, AllowUnexported, or cmpopts.IgnoreUnexported
Run Code Online (Sandbox Code Playgroud)

如何修复此错误并比较变量?

comparison struct go go-cmp

5
推荐指数
1
解决办法
4008
查看次数

标签 统计

go ×2

go-cmp ×2

struct ×2

comparison ×1