cmp

package
v0.0.0-...-f2d555a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2026 License: BSD-2-Clause, BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package cmp provides types and functions related to comparing ordered values.

It is a copy of cmp with some additions. It is a strict superset and can be used as a drop-in replacement for the standard library package everywhere.

Index

Constants

View Source
const (
	// OrderLess indicates that the first value is less than the second value.
	// It is the same value (-1) as returned by [cmp.Compare].
	OrderLess = Order(-1)

	// OrderEqual indicates that the first value is equal to the second value.
	// It is the same value (0) as returned by [cmp.Compare].
	OrderEqual = Order(0)

	// OrderGreater indicates that the first value is greater than the second value.
	// It is the same value (+1) as returned by [cmp.Compare].
	OrderGreater = Order(+1)
)

Variables

This section is empty.

Functions

func Compare

func Compare[T Ordered](x, y T) int

Compare returns

-1 ([OrderLess])    if x is less than y,
 0 ([OrderEqual])   if x equals y,
+1 ([OrderGreater]) if x is greater than y.

For floating-point types, a NaN is considered less than any non-NaN, a NaN is considered equal to a NaN, and -0.0 is equal to 0.0.

It is the same as cmp.Compare.

func Equal

func Equal[T Ordered](x, y T) bool

Equal reports whether x is equal to y. For floating-point types, a NaN is considered equal to a NaN, and -0.0 is equal to 0.0.

func Greater

func Greater[T Ordered](x, y T) bool

Greater reports whether x is greater than y. For floating-point types, any non-NaN is considered greater than a NaN, and -0.0 is not greater than (is equal to) 0.0.

func Less

func Less[T Ordered](x, y T) bool

Less reports whether x is less than y. For floating-point types, a NaN is considered less than any non-NaN, and -0.0 is not less than (is equal to) 0.0.

It is the same as cmp.Less.

func Or

func Or[T comparable](vals ...T) T

Or returns the first of its arguments that is not equal to the zero value. If no argument is non-zero, it returns the zero value.

It is the same as cmp.Or.

Types

type Order

type Order int

Order represents the result of a comparison between two values.

func (Order) String

func (o Order) String() string

String implements fmt.Stringer.

type Ordered

type Ordered = cmp.Ordered

Ordered is a constraint that permits any ordered type: any type that supports the operators < <= >= >. If future releases of Go add new ordered types, this constraint will be modified to include them.

Note that floating-point types may contain NaN ("not-a-number") values. An operator such as == or < will always report false when comparing a NaN value with any other value, NaN or not. See the Compare function for a consistent way to compare NaN values.

It is a type alias for cmp.Ordered.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL