whatisgithub

What is go-cmp?

google/go-cmp — explained in plain English

Analysis updated 2026-06-26

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

In one sentence

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

What do people build with it?

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.

What is it built with?

Go

How does it compare?

google/go-cmpantoniomika/sishnginx-proxy/docker-gen
Stars4,6304,6264,625
LanguageGoGoGo
Setup difficultyeasymoderatemoderate
Complexity2/53/53/5
Audiencedeveloperops devopsops devops

Figures from each repo's GitHub metadata at analysis time.

How do you get 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.

So what is it?

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?

Frequently asked questions

What is go-cmp?

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.

What language is go-cmp written in?

Mainly Go. The stack also includes Go.

What license does go-cmp use?

Use freely for any purpose, including commercial use, as long as you keep the copyright notice.

How hard is go-cmp to set up?

Setup difficulty is rated easy, with roughly 5min to a first successful run.

Who is go-cmp for?

Mainly developer.

Open on GitHub → Ask about another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.