DEV.co
Open-Source Testing · google

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.

Source: GitHub — github.com/google/go-cmp
4.7k
GitHub stars
224
Forks
Go
Primary language
BSD-3-Clause
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositorygoogle/go-cmp
Ownergoogle
Primary languageGo
LicenseBSD-3-Clause — OSI-approved
Stars4.7k
Forks224
Open issues57
Latest releasev0.7.0 (2025-02-21)
Last updated2026-06-18
Sourcehttps://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.

Quickstart

Get the go-cmp source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/google/go-cmp.gitcd go-cmp# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Unit Testing with Complex Structs

Use go-cmp when comparing complex nested structs, slices, and maps in unit tests where reflect.DeepEqual produces panics or unhelpful error messages. Custom equality functions enable tolerance-based float comparison and field-level filtering.

Tolerance-Based Numeric Comparisons

Define custom equality functions to compare floating-point numbers within acceptable tolerances, avoiding flaky tests due to precision issues. Ideal for numerical computation, scientific simulations, and financial calculations in test suites.

API Response and Data Structure Validation

Compare deserialized JSON/protobuf structs in integration and end-to-end tests, selectively ignoring timestamps, IDs, or other dynamic fields while validating critical data. Provides clearer test failures than reflect.DeepEqual.

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.

SignalAssessment
MaintenanceActive
DocumentationStrong
License clarityClear
Deployment complexityLow
DEV.co fitGood
Assessment confidenceHigh
Security considerations

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.

Software development agency

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.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

Related 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?
Technically yes, but it is purpose-built for testing. Production code should use appropriate data structure libraries or custom logic. No performance guarantees for non-test use.
Does go-cmp support comparing unexported fields by default?
No. By default, unexported fields are ignored to prevent panics. Use cmpopts.AllowUnexported or cmpopts.IgnoreUnexported to control behavior explicitly.
How do I handle floating-point tolerance comparisons?
Define a custom equality function using cmp.FilterValues and pass it to cmp.Diff via cmp.Options. The documentation includes float tolerance examples.
Is go-cmp maintained by Google?
Yes, it is a Google-authored open-source project, though marked 'This is not an official Google product'. Active maintenance as of 2026 with regular releases.

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`.