explaingit

google/go-cmp

4,630GoAudience · developerComplexity · 2/5LicenseSetup · easy

TLDR

A Go library from Google for comparing two values in tests and showing exactly how they differ, with support for custom equality rules, floating-point tolerances, and unexported field control.

Mindmap

mindmap
  root((go-cmp))
    What it does
      Value comparison
      Human-readable diffs
      Custom equality rules
    Features
      Float tolerance
      Unexported field control
      Type-specific options
    Use cases
      Test assertions
      Struct comparison
      Debug failing tests
    Audience
      Go developers
      Backend teams
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Replace reflect.DeepEqual in Go tests to get a human-readable diff showing exactly what changed.

USE CASE 2

Write custom equality rules for specific types, such as treating two floating-point numbers as equal when they are very close.

USE CASE 3

Compare complex nested structs in tests without crashes on nil pointers or accidental matches on private fields.

Tech stack

Go

Getting it running

Difficulty · easy Time to first run · 5min
Use freely for any purpose, including commercial use, as long as you keep the copyright notice.

In plain English

go-cmp is a small Go library from Google for checking whether two values are equal in tests. Go is a programming language, and when you write automated tests for Go code, you often need to compare an expected result against an actual result to see if they match. The standard approach built into Go for doing this comparison has some limitations: it does not handle all data types well, it can crash on certain kinds of values, and it gives you no way to define what "equal" means for your own types. This library was written to address those problems. With go-cmp, you can compare two values and get a clear description of exactly how they differ, which makes it easier to understand a failing test. You can also provide custom rules for specific types, for example telling the library to treat two floating-point numbers as equal if they are very close to each other rather than requiring them to be identical. Types that define their own equality method will have that method respected automatically. One deliberate difference from the built-in Go comparison: go-cmp does not compare private (unexported) fields inside a type unless you explicitly allow it, which avoids a class of accidental test failures and forces you to think about what your test is actually checking. Installation is a single command using Go's standard package manager.

Copy-paste prompts

Prompt 1
Show me how to use go-cmp to compare two structs in a Go test and print a human-readable diff of the differences.
Prompt 2
I have a Go test comparing float64 values. Use go-cmp to define a custom comparer that treats values within 0.001 as equal.
Prompt 3
Replace all reflect.DeepEqual calls in my Go test file with cmp.Equal and add a cmp.Option to ignore unexported fields.
Prompt 4
How do I use go-cmp to compare only the exported fields of a struct while explicitly ignoring private implementation details?
Open on GitHub → Explain another repo

← google on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.