Go cmp: compare ordered values
Answer: Package cmp provides types and functions related to comparing ordered values.
The Ordered constraint
Ordered is a constraint that permits any ordered type.
That means a type that supports the operators <, <=, >=, and >.
If later Go releases add new ordered types, the constraint will be modified to include them.
For floating-point types, a NaN is considered less than any non-NaN.
Compare, Less, and Or
Compare returns -1 if x is less than y.
It also returns 0 if x equals y, and +1 if x is greater than y.
Less reports whether x is less than y.
Or returns the first argument that is not the zero value.
See also
Claims
- id: C1 claim: Package cmp provides types and functions related to comparing ordered values. source: S1 evidence: "types and functions related to comparing ordered values"
- id: C2 claim: Compare returns -1 if x is less than y. source: S1 evidence: "Compare returns -1 if x is less than y"
- id: C3 claim: Ordered is a constraint that permits any ordered type. source: S2 evidence: "Ordered is a constraint that permits any ordered type"
- id: C4 claim: Less reports whether x is less than y. source: S2 evidence: "Less reports whether x is less than y"
Sources
- [S1] cmp. The Go Authors. https://pkg.go.dev/cmp . License: BSD-3-Clause. Accessed 2026-11-26.
- [S2] cmp source. The Go Authors. https://go.dev/src/cmp/cmp.go . License: BSD-3-Clause. Accessed 2026-11-26.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Go Authors, cmp; The Go Authors, cmp source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Go Authors, cmp; The Go Authors, cmp source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "Go cmp: compare ordered values." iinventedlight Library, iil:note:m5xkb0br, version of 2026-11-26. https://iinventedlight.com/library/notes/go-cmp/. License: CC-BY-4.0.
@misc{iil_note_m5xkb0br,
author = {{iinventedlight}},
title = {{Go cmp: compare ordered values}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/go-cmp/},
note = {Permanent ID iil:note:m5xkb0br. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- go-cmp.schema.json (schema; CC-BY-4.0)
- go-cmp.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-cmp.md.