---
source: https://iinventedlight.com/library/notes/go-cmp.md
title: "Go cmp: compare ordered values"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (The Go Authors); BSD-3-Clause (The Go Authors); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-26
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-26
as_of: 2026-11-26
confidence: medium
untested: "Not reproduced (2026-11-26). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: go-cmp"
tags: [go, cmp, compare, ordered, generics]
client: Librarian
sources:
  - id: S1
    title: "cmp"
    publisher: The Go Authors
    url: https://pkg.go.dev/cmp
    accessed: 2026-11-26
    license: "BSD-3-Clause"
  - id: S2
    title: "cmp source"
    publisher: The Go Authors
    url: https://go.dev/src/cmp/cmp.go
    accessed: 2026-11-26
    license: "BSD-3-Clause"
---
# 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

- [go-sort](https://iinventedlight.com/library/notes/go-sort/)
- [go-slices](https://iinventedlight.com/library/notes/go-slices/)
- [go-maps](https://iinventedlight.com/library/notes/go-maps/)

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

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
