go-cmp
go-cmp is a Go testing library that provides a more flexible and safer alternative to the standard reflect.DeepEqual function. It allows developers to define custom equality rules, compare unexported fields safely, and write clearer test assertions for complex data structures.
Key facts
Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.
| Field | Value |
|---|---|
| Repository | google/go-cmp |
| Owner | |
| Primary language | Go |
| License | BSD-3-Clause — OSI-approved |
| Stars | 4.7k |
| Forks | 224 |
| Open issues | 57 |
| Latest release | v0.7.0 (2025-02-21) |
| Last updated | 2026-06-18 |
| Source | https://github.com/google/go-cmp |
What go-cmp is
go-cmp enables semantic equality comparison of Go values through customizable equality functions, support for user-defined Equal methods, and recursive field comparison with explicit control over unexported fields. It addresses reflect.DeepEqual limitations by preventing panics and offering fine-grained comparison options via cmpopts.
Get the go-cmp source
Clone the repository and explore it locally.
git clone https://github.com/google/go-cmp.gitcd go-cmp# follow the project's README for install & configurationNeed it deployed, integrated, or customized instead? DEV.co ships production installs.
Best use cases
Implementation considerations
- Familiarize yourself with cmpopts package for common patterns like IgnoreUnexported, IgnoreFields, and SortSlices to avoid boilerplate in test setup.
- Define reusable custom equality functions as package-level variables (e.g., floatTolerance) to keep test code DRY and maintainable.
- Use the Diff() function in test failures to generate human-readable output; integrate with your test framework's assertion library for consistent error reporting.
- Be explicit about comparing unexported fields; use AllowUnexported sparingly and only when necessary, as it couples tests to internal implementation details.
- Profile test performance if comparing very large data structures; consider filtering or sampling in performance-sensitive test suites.
When to avoid it — and what to weigh
- Performance-Critical Runtime Comparisons — go-cmp is designed for test code. Do not use in production hot paths or performance-sensitive code where reflect.DeepEqual or simple equality operators are insufficient; benchmark overhead is not a primary design concern.
- Non-Go Projects — go-cmp is Go-specific and cannot be integrated into Python, Rust, Java, or other language ecosystems without re-implementation or language bindings.
- Serialization or Data Transfer — go-cmp is a comparison library, not a serialization framework. Do not use for marshaling, encoding, or transmitting data; it operates on in-memory Go values only.
- Backward Compatibility Across Major Go Versions — If your project must support Go versions significantly older than the latest stable release, review go-cmp's minimum Go version requirement; older versions may not be maintained.
License & commercial use
BSD-3-Clause (New or Revised). This is a permissive OSI-approved open-source license. Allows commercial use, modification, and distribution with minimal restrictions; requires inclusion of license and copyright notice.
BSD-3-Clause explicitly permits commercial use without restrictions or royalty obligations. You may use go-cmp in proprietary Go applications and redistribute compiled binaries. Always include the original license file with source code distributions. No ambiguity; review the LICENSE file in the repository for exact terms.
DEV.co evaluation signals
Editorial assessment — not user reviews. Directional, with an explicit confidence level.
| Signal | Assessment |
|---|---|
| Maintenance | Active |
| Documentation | Strong |
| License clarity | Clear |
| Deployment complexity | Low |
| DEV.co fit | Good |
| Assessment confidence | High |
No known public security vulnerabilities reported. Library performs in-memory value comparison only; no file I/O, network, or privilege-escalation code paths. Unexported field handling is explicit, reducing accidental data leaks in tests. Dependency on Go standard library only. No cryptographic operations or sensitive data handling; security posture typical of a testing utility.
Alternatives to consider
reflect.DeepEqual (Go standard library)
No external dependency, but causes panics on unexported fields and offers no customization. go-cmp is safer and more flexible for complex comparisons.
testify/assert (github.com/stretchr/testify)
Comprehensive assertion library with built-in Equal and EqualValues helpers, but less flexible for custom equality logic and tolerance-based comparisons. Heavier dependency if you only need comparison logic.
go-test/deep (github.com/go-test/deep)
Simpler, lighter-weight alternative focusing on deep equality and diff output, but lacks go-cmp's custom equality functions and explicit field control.
Build on go-cmp with DEV.co software developers
Replace reflect.DeepEqual with go-cmp for safer, more expressive test comparisons. Define custom equality logic, control unexported fields, and get readable diffs. Install now with `go get github.com/google/go-cmp/cmp`.
Talk to DEV.coRelated on DEV.co
Explore the category and the services that help you build with it.
go-cmp FAQ
Can I use go-cmp outside of tests?
Does go-cmp support comparing unexported fields by default?
How do I handle floating-point tolerance comparisons?
Is go-cmp maintained by Google?
Software developers & web developers for hire
From first prototype to production, DEV.co delivers software development services around tools like go-cmp. Our software development agency staffs experienced software developers and web developers for custom software development, web development, integrations, and ongoing support across open-source testing and beyond.
Simplify Go Test Assertions with go-cmp
Replace reflect.DeepEqual with go-cmp for safer, more expressive test comparisons. Define custom equality logic, control unexported fields, and get readable diffs. Install now with `go get github.com/google/go-cmp/cmp`.