cmp

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package cmp provides types and functions related to comparing ordered values. Based on the cmp package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

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

Compare returns

-1 if x is less than y,
 0 if x equals y,
+1 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.

Example
package main

import (
	"solod.dev/so/cmp"
	"solod.dev/so/fmt"
)

func main() {
	fmt.Printf("%d\n", cmp.Compare(1, 2))
	fmt.Printf("%d\n", cmp.Compare("a", "aa"))
	fmt.Printf("%d\n", cmp.Compare(1.5, 1.5))
}
Output:
-1
-1
0

func Equal

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

Equal reports whether x and y are equal. For floating-point types, a NaN is considered equal to a NaN, and -0.0 is equal to 0.0. For non-ordered types, compares by raw byte value (memcmp).

Example
package main

import (
	"solod.dev/so/cmp"
	"solod.dev/so/fmt"
)

func main() {
	fmt.Printf("%t\n", cmp.Equal(1, 1))
	fmt.Printf("%t\n", cmp.Equal("a", "aa"))
	fmt.Printf("%t\n", cmp.Equal(1.5, 1.5))
}
Output:
true
false
true

func Less

func Less[T cmp.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.

Example
package main

import (
	"solod.dev/so/cmp"
	"solod.dev/so/fmt"
)

func main() {
	fmt.Printf("%t\n", cmp.Less(1, 2))
	fmt.Printf("%t\n", cmp.Less("a", "aa"))
}
Output:
true
true

Types

type Func

type Func func(a, b any) int

Func is a comparison function that returns a negative value if a < b, zero if a == b, and a positive value if a > b.

func FuncFor

func FuncFor[T any]() Func

FuncFor returns the appropriate comparison function for type T. If T is not supported, returns nil.

Jump to

Keyboard shortcuts

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