summaryrefslogtreecommitdiff
path: root/devdocs/go/cmp%2Findex.html
blob: d13c7f2c747a1d1114d6fba9c58bc2ac95621e7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<h1> Package cmp  </h1>     <ul id="short-nav">
<li><code>import "cmp"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
</ul>     <h2 id="pkg-overview">Overview </h2> <p>Package cmp provides types and functions related to comparing ordered values. </p>     <h2 id="pkg-index">Index </h2>  <ul id="manual-nav">
<li><a href="#Compare">func Compare[T Ordered](x, y T) int</a></li>
<li><a href="#Less">func Less[T Ordered](x, y T) bool</a></li>
<li><a href="#Or">func Or[T comparable](vals ...T) T</a></li>
<li><a href="#Ordered">type Ordered</a></li>
</ul> <div id="pkg-examples"> <h3>Examples</h3>  <dl> <dd><a class="exampleLink" href="#example_Or">Or</a></dd> <dd><a class="exampleLink" href="#example_Or_sort">Or (Sort)</a></dd> </dl> </div> <h3>Package files</h3> <p>  <span>cmp.go</span>  </p>   <h2 id="Compare">func <span>Compare</span>  </h2> <pre data-language="go">func Compare[T Ordered](x, y T) int</pre> <p>Compare returns </p>
<pre data-language="go">-1 if x is less than y,
 0 if x equals y,
+1 if x is greater than y.
</pre> <p>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. </p>
<h2 id="Less">func <span>Less</span>  </h2> <pre data-language="go">func Less[T Ordered](x, y T) bool</pre> <p>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. </p>
<h2 id="Or">func <span>Or</span>  </h2> <pre data-language="go">func Or[T comparable](vals ...T) T</pre> <p>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. </p>   <h4 id="example_Or"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// 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"))
</pre> <p>Output:</p> <pre class="output" data-language="go">default
some text
some text
</pre>      <h4 id="example_Or_sort"> <span class="text">Example (Sort)</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">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)
}

</pre> <p>Output:</p> <pre class="output" data-language="go">foo alice 2.00
foo alice 1.00
bar bob 3.00
foo bob 4.00
bar carol 1.00
baz carol 4.00
</pre>   <h2 id="Ordered">type <span>Ordered</span>  <span title="Added in Go 1.21">1.21</span> </h2> <p>Ordered is a constraint that permits any ordered type: any type that supports the operators &lt; &lt;= &gt;= &gt;. If future releases of Go add new ordered types, this constraint will be modified to include them. </p>
<p>Note that floating-point types may contain NaN ("not-a-number") values. An operator such as == or &lt; will always report false when comparing a NaN value with any other value, NaN or not. See the <a href="#Compare">Compare</a> function for a consistent way to compare NaN values. </p>
<pre data-language="go">type Ordered interface {
    ~int | ~int8 | ~int16 | ~int32 | ~int64 |
        ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
        ~float32 | ~float64 |
        ~string
}</pre><div class="_attribution">
  <p class="_attribution-p">
    &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
    <a href="http://golang.org/pkg/cmp/" class="_attribution-link">http://golang.org/pkg/cmp/</a>
  </p>
</div>