go-test/deep
GitHub: go-test/deep
一个 Go 语言的深度变量相等性比较库,在比较失败时返回人类可读的差异列表而非简单的布尔值。
Stars: 789 | Forks: 56
# 为人类设计的深度变量相等性比较
[](https://goreportcard.com/report/github.com/go-test/deep)
[](https://coveralls.io/github/go-test/deep?branch=master)
[](https://pkg.go.dev/github.com/go-test/deep)
这个包提供了一个简单的函数:`deep.Equal`。它类似于 [reflect.DeepEqual](https://pkg.go.dev/reflect#DeepEqual),但对人类更友好,原因有两点:
* `deep.Equal` 会返回差异列表
* `deep.Equal`(默认情况下)不比较未导出的字段
`reflect.DeepEqual` 很好(就像所有 Golang 的东西一样!),但它就像在玩 [Hunt the Wumpus](https://en.wikipedia.org/wiki/Hunt_the_Wumpus) 游戏。对于大型的 map、slice 和 struct 来说,找出差异是非常困难的。
`deep.Equal` 不会跟你玩捉迷藏,它会直接列出差异:
```
package main_test
import (
"testing"
"github.com/go-test/deep"
)
type T struct {
Name string
Numbers []float64
}
func TestDeepEqual(t *testing.T) {
// Can you spot the difference?
t1 := T{
Name: "Isabella",
Numbers: []float64{1.13459, 2.29343, 3.010100010},
}
t2 := T{
Name: "Isabella",
Numbers: []float64{1.13459, 2.29843, 3.010100010},
}
if diff := deep.Equal(t1, t2); diff != nil {
t.Error(diff)
}
}
```
```
$ go test
--- FAIL: TestDeepEqual (0.00s)
main_test.go:25: [Numbers.slice[1]: 2.29343 != 2.29843]
```
差异在于 `Numbers.slice[1]`:这两个值在使用 Go 的 `==` 进行比较时并不相等。
标签:EVTX分析, 日志审计