go-cmp is a Go library for comparing values. It is typically used in unit tests. Use this tag together with [go] tag.
go-cmp is a library for comparing values. Due to performance trade-offs, it is best suited for non-production code, like unit tests. go-cmp can make it easier to computing diffs and compare values that aren't directly comparable with ==
and !=
as per the Go spec.
Useful links:
- Package documentation: https://pkg.go.dev/github.com/google/go-cmp
- Project repository: https://github.com/google/go-cmp
Basic example:
package main
import (
"fmt"
"github.com/google/go-cmp/cmp"
)
type Foo struct {
Name string
Values []int
}
func main() {
a := Foo{"Foo1", []int{1, 2, 3}}
b := Foo{"Foo1", []int{1, 2, 3}}
c := Foo{"Foo1", []int{1, 2, 5}}
fmt.Println("is a equal to b?", cmp.Equal(a, b)) // true
fmt.Println("is b equal to c?", cmp.Equal(b, c)) // false
}