AlgoMaster Logo

The cmp Package

Last Updated: May 22, 2026

Low Priority
11 min read

The cmp package, added in Go 1.21, gives you a generic vocabulary for ordering values. It defines the Ordered constraint that slices.Sort and friends rely on, a pair of comparison functions that handle floating-point NaN cleanly, and (since Go 1.22) cmp.Or for picking the first non-zero value out of a list. The package is small, with three top-level functions and one constraint, but it cleans up a class of code that used to require either reflection or hand-written Less functions for every type. Most production Go projects pick it up the day they upgrade past 1.21, because it removes boilerplate from every sort and every "pick a default" expression.

Premium Content

Subscribe to unlock full access to this content and more premium articles.