go-cmp compares Go values in tests with safer behavior than reflect.DeepEqual. You can plug in custom equality logic and respect types that define Equal methods.
Overview
go-cmp is a Go package aimed at test code that needs to decide whether two values are semantically equal. It offers a more capable and safer option than reflect.DeepEqual for that job.
When default equality is not enough, you can supply custom equality functions, for example treating floats as equal within a tolerance. Types that implement an Equal method can use that method for comparison decisions.
Without custom functions or Equal methods, comparison walks primitive fields recursively in a DeepEqual-like way. Unexported fields are not compared by default and can panic unless you use options such as IgnoreUnexported or explicit AllowUnexported comparison.
You import the package into Go test or application code; it is not a standalone runnable tool.
Key features
- Semantic equality for Go values
- Custom equality function overrides
- Support for type Equal methods
- Recursive primitive comparison default
- IgnoreUnexported comparison option
- AllowUnexported field comparison option
- Test-focused comparison API
- Alternative to reflect.DeepEqual
Use cases
- Asserting struct equality in Go tests
- Comparing floats with tolerance
- Testing types with Equal methods
- Comparing values with unexported fields
- Structured test failure diffs