From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/go/cmp%2Findex.html | 73 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 devdocs/go/cmp%2Findex.html (limited to 'devdocs/go/cmp%2Findex.html') diff --git a/devdocs/go/cmp%2Findex.html b/devdocs/go/cmp%2Findex.html new file mode 100644 index 00000000..d13c7f2c --- /dev/null +++ b/devdocs/go/cmp%2Findex.html @@ -0,0 +1,73 @@ +

Package cmp

Overview

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

Index

Examples

Or
Or (Sort)

Package files

cmp.go

func Compare

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

+

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.

+

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.

Example +

Code:

// Suppose we have some user input
+// that may or may not be an empty string
+userInput1 := ""
+userInput2 := "some text"
+
+fmt.Println(cmp.Or(userInput1, "default"))
+fmt.Println(cmp.Or(userInput2, "default"))
+fmt.Println(cmp.Or(userInput1, userInput2, "default"))
+

Output:

default
+some text
+some text
+

Example (Sort) +

Code:

type Order struct {
+    Product  string
+    Customer string
+    Price    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 price
+slices.SortFunc(orders, func(a, b Order) int {
+    return cmp.Or(
+        cmp.Compare(a.Customer, b.Customer),
+        cmp.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.00
+foo alice 1.00
+bar bob 3.00
+foo bob 4.00
+bar carol 1.00
+baz carol 4.00
+

type Ordered 1.21

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.

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

+ © Google, Inc.
Licensed under the Creative Commons Attribution License 3.0.
+ http://golang.org/pkg/cmp/ +

+
-- cgit v1.2.3