Movatterモバイル変換


[0]ホーム

URL:


cmp

packagestandard library
go1.25.5Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License:BSD-3-ClauseImports:0Imported by:7,792

Details

Repository

cs.opensource.google/go/go

Links

Documentation

Overview

Package cmp provides types and functions related to comparingordered values.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

funcCompare

func Compare[TOrdered](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 mainimport ("cmp""fmt""math")func main() {fmt.Println(cmp.Compare(1, 2))fmt.Println(cmp.Compare("a", "aa"))fmt.Println(cmp.Compare(1.5, 1.5))fmt.Println(cmp.Compare(math.NaN(), 1.0))}
Output:-1-10-1

funcLess

func Less[TOrdered](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 mainimport ("cmp""fmt""math")func main() {fmt.Println(cmp.Less(1, 2))fmt.Println(cmp.Less("a", "aa"))fmt.Println(cmp.Less(1.0, math.NaN()))fmt.Println(cmp.Less(math.NaN(), 1.0))}
Output:truetruefalsetrue

funcOradded ingo1.22.0

func Or[Tcomparable](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.

Example
package mainimport ("cmp""fmt")func main() {// Suppose we have some user input// that may or may not be an empty stringuserInput1 := ""userInput2 := "some text"fmt.Println(cmp.Or(userInput1, "default"))fmt.Println(cmp.Or(userInput2, "default"))fmt.Println(cmp.Or(userInput1, userInput2, "default"))}
Output:defaultsome textsome text

Example (Sort)
package mainimport ("cmp""fmt""slices""strings")func main() {type Order struct {Product  stringCustomer stringPrice    float64}orders := []Order{{"foo", "alice", 1.00},{"bar", "bob", 3.00},{"baz", "carol", 4.00},{"foo", "alice", 2.00},{"bar", "carol", 1.00},{"foo", "bob", 4.00},}// Sort by customer first, product second, and last by higher priceslices.SortFunc(orders, func(a, b Order) int {return cmp.Or(strings.Compare(a.Customer, b.Customer),strings.Compare(a.Product, b.Product),cmp.Compare(b.Price, a.Price),)})for _, order := range orders {fmt.Printf("%s %s %.2f\n", order.Product, order.Customer, order.Price)}}
Output:foo alice 2.00foo alice 1.00bar bob 3.00foo bob 4.00bar carol 1.00baz carol 4.00

Types

typeOrdered

type Ordered interface {~int | ~int8 | ~int16 | ~int32 | ~int64 |~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |~float32 | ~float64 |~string}

Ordered is a constraint that permits any ordered type: any typethat 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 whencomparing a NaN value with any other value, NaN or not.See theCompare function for a consistent way to compare NaN values.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp