|
1 | 1 | <!--{
|
2 | 2 | "Title": "The Go Programming Language Specification",
|
3 |
| - "Subtitle": "Version of May 24, 2023", |
| 3 | + "Subtitle": "Version of May 25, 2023", |
4 | 4 | "Path": "/ref/spec"
|
5 | 5 | }-->
|
6 | 6 |
|
@@ -643,6 +643,7 @@ <h2 id="Constants">Constants</h2>
|
643 | 643 | a <a href="#Constant_expressions">constant expression</a>,
|
644 | 644 | a <a href="#Conversions">conversion</a> with a result that is a constant, or
|
645 | 645 | the result value of some built-in functions such as
|
| 646 | +<code>min</code> or <code>max</code> applied to constant arguments, |
646 | 647 | <code>unsafe.Sizeof</code> applied to <a href="#Package_unsafe">certain values</a>,
|
647 | 648 | <code>cap</code> or <code>len</code> applied to
|
648 | 649 | <a href="#Length_and_capacity">some expressions</a>,
|
@@ -2319,7 +2320,7 @@ <h3 id="Predeclared_identifiers">Predeclared identifiers</h3>
|
2319 | 2320 |
|
2320 | 2321 | Functions:
|
2321 | 2322 | append cap clear close complex copy delete imag len
|
2322 |
| - make new panic print println real recover |
| 2323 | + make max min new panic print println real recover |
2323 | 2324 | </pre>
|
2324 | 2325 |
|
2325 | 2326 | <h3 id="Exported_identifiers">Exported identifiers</h3>
|
@@ -7531,6 +7532,70 @@ <h3 id="Making_slices_maps_and_channels">Making slices, maps and channels</h3>
|
7531 | 7532 | </p>
|
7532 | 7533 |
|
7533 | 7534 |
|
| 7535 | +<h3 id="Min_and_max">Min and max</h3> |
| 7536 | + |
| 7537 | +<p> |
| 7538 | +The built-in functions <code>min</code> and <code>max</code> compute the |
| 7539 | +smallest—or largest, respectively—value of a fixed number of |
| 7540 | +arguments of <a href="#Comparison_operators">ordered types</a>. |
| 7541 | +There must be at least one argument. |
| 7542 | +</p> |
| 7543 | + |
| 7544 | +<p> |
| 7545 | +The same type rules as for <a href="#Operators">operators</a> apply: |
| 7546 | +for <a href="#Comparison_operators">ordered</a> arguments <code>x</code> and |
| 7547 | +<code>y</code>, <code>min(x, y)</code> is valid if <code>x + y</code> is valid, |
| 7548 | +and the type of <code>min(x, y)</code> is the type of <code>x + y</code> |
| 7549 | +(and similarly for <code>max</code>). |
| 7550 | +If all arguments are constant, the result is constant. |
| 7551 | +</p> |
| 7552 | + |
| 7553 | +<pre> |
| 7554 | +var x, y int |
| 7555 | +m := min(x) // m == x |
| 7556 | +m := min(x, y) // m is the smaller of x and y |
| 7557 | +m := max(x, y, 10) // m is the larger of x and y but at least 10 |
| 7558 | +c := max(1, 2.0, 10) // c == 10.0 (floating-point kind) |
| 7559 | +f := max(0, float32(x)) // type of f is float32 |
| 7560 | +var s []string |
| 7561 | +_ = min(s...) // invalid: slice arguments are not permitted |
| 7562 | +t := max("", "foo", "bar") // t == "foo" (string kind) |
| 7563 | +</pre> |
| 7564 | + |
| 7565 | +<p> |
| 7566 | +For numeric arguments, <code>min</code> and <code>max</code> are |
| 7567 | +commutative and associative: |
| 7568 | +</p> |
| 7569 | + |
| 7570 | +<pre> |
| 7571 | +min(x, y) == min(y, x) |
| 7572 | +min(x, y, z) == min(min(x, y), z) == min(x, min(y, z)) |
| 7573 | +</pre> |
| 7574 | + |
| 7575 | +<p> |
| 7576 | +For floating-point arguments negative zero, NaN, and infinity the following rules apply: |
| 7577 | +</p> |
| 7578 | + |
| 7579 | +<pre> |
| 7580 | + x y min(x, y) max(x, y) |
| 7581 | + |
| 7582 | + -0.0 0.0 -0.0 0.0 // negative zero is smaller than (non-negative) zero |
| 7583 | + -Inf y -Inf y // negative infinity is smaller than any other number |
| 7584 | + +Inf y y +Inf // positive infinity is larger than any other number |
| 7585 | + NaN y NaN NaN // if any argument is a NaN, the result is a NaN |
| 7586 | +</pre> |
| 7587 | + |
| 7588 | +<p> |
| 7589 | +For string arguments the result for <code>min</code> is the first argument |
| 7590 | +with the smallest (or for <code>max</code>, largest) value, |
| 7591 | +compared lexically byte-wise: |
| 7592 | +</p> |
| 7593 | + |
| 7594 | +<pre> |
| 7595 | +min(x, y) == if x <= y then x else y |
| 7596 | +min(x, y, z) == min(min(x, y), z) |
| 7597 | +</pre> |
| 7598 | + |
7534 | 7599 | <h3 id="Allocation">Allocation</h3>
|
7535 | 7600 |
|
7536 | 7601 | <p>
|
|
0 commit comments