|
1 | 1 | <!--{
|
2 | 2 | "Title": "The Go Programming Language Specification",
|
3 |
| - "Subtitle": "Language version go1.22 (April 25, 2024)", |
| 3 | + "Subtitle": "Language version go1.23 (June 4, 2024)", |
4 | 4 | "Path": "/ref/spec"
|
5 | 5 | }-->
|
6 | 6 |
|
@@ -6656,13 +6656,16 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
|
6656 | 6656 | </p>
|
6657 | 6657 |
|
6658 | 6658 | <pre class="grammar">
|
6659 |
| -Range expression 1st value 2nd value |
| 6659 | +Range expression 1st value 2nd value |
6660 | 6660 |
|
6661 |
| -array or slice a [n]E, *[n]E, or []E index i int a[i] E |
6662 |
| -string s string type index i int see below rune |
6663 |
| -map m map[K]V key k K m[k] V |
6664 |
| -channel c chan E, <-chan E element e E |
6665 |
| -integer value n integer type, or untyped int value i see below |
| 6661 | +array or slice a [n]E, *[n]E, or []E index i int a[i] E |
| 6662 | +string s string type index i int see below rune |
| 6663 | +map m map[K]V key k K m[k] V |
| 6664 | +channel c chan E, <-chan E element e E |
| 6665 | +integer value n integer type, or untyped int value i see below |
| 6666 | +function, 0 values f func(func() bool) |
| 6667 | +function, 1 value f func(func(V) bool) value v V |
| 6668 | +function, 2 values f func(func(K, V) bool) key k K v V |
6666 | 6669 | </pre>
|
6667 | 6670 |
|
6668 | 6671 | <ol>
|
@@ -6716,6 +6719,23 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
|
6716 | 6719 | the type of the iteration values is the <a href="#Constants">default type</a> for <code>n</code>.
|
6717 | 6720 | If <code>n</code> <= 0, the loop does not run any iterations.
|
6718 | 6721 | </li>
|
| 6722 | + |
| 6723 | +<li> |
| 6724 | +For a function <code>f</code>, the iteration proceeds by calling <code>f</code> |
| 6725 | +with a new, synthesized <code>yield</code> function as its argument. |
| 6726 | +If <code>yield</code> is called before <code>f</code> returns, |
| 6727 | +the arguments to <code>yield</code> become the iteration values |
| 6728 | +for executing the loop body once. |
| 6729 | +After each successive loop iteration, <code>yield</code> returns true |
| 6730 | +and may be called again to continue the loop. |
| 6731 | +As long as the loop body does not terminate, the "range" clause will continue |
| 6732 | +to generate iteration values this way for each <code>yield</code> call until |
| 6733 | +<code>f</code> returns. |
| 6734 | +If the loop body terminates (such as by a <code>break</code> statement), |
| 6735 | +<code>yield</code> returns false and must not be called again. |
| 6736 | +The number of iteration variables must match the number and order of arguments |
| 6737 | +to <code>yield</code>. |
| 6738 | +</li> |
6719 | 6739 | </ol>
|
6720 | 6740 |
|
6721 | 6741 | <p>
|
@@ -6784,6 +6804,16 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
|
6784 | 6804 | // invalid: 1e3 is a floating-point constant
|
6785 | 6805 | for range 1e3 {
|
6786 | 6806 | }
|
| 6807 | +<!-- TODO(gri) need better examples for range-over-func --> |
| 6808 | +// print hello world |
| 6809 | +f := func(yield func(string) bool) { |
| 6810 | + if yield("hello") { |
| 6811 | + yield("world") |
| 6812 | + } |
| 6813 | +} |
| 6814 | +for word := range f { |
| 6815 | + println(word) |
| 6816 | +} |
6787 | 6817 | </pre>
|
6788 | 6818 |
|
6789 | 6819 |
|
|
0 commit comments