cmp
Coreimport "cmp"Provides types and functions related to ordered comparison (Go 1.21+).
Example
package main
import (
"cmp"
"fmt"
)
func main() {
fmt.Println(cmp.Compare(3, 5)) // -1
fmt.Println(cmp.Compare(5, 5)) // 0
fmt.Println(cmp.Or(0, 42)) // 42
}Key Types & Functions
CompareLessOrOrderedAbout cmp
The cmp package (imported as cmp) belongs to the Core category of Go packages. Provides types and functions related to ordered comparison (Go 1.21+).
Go's standard library is one of the language's greatest strengths, providing production-ready implementations for networking, cryptography, encoding, I/O, and more. The cmp package follows Go's philosophy of simplicity and composability — small, focused packages that combine through interfaces like io.Reader and io.Writer.
When using cmp in production, follow Go best practices: handle errors explicitly, use context for cancellation and timeouts, prefer composition over inheritance, and write table-driven tests. The Go documentation at pkg.go.dev provides comprehensive API references and examples for every exported type and function.