|
1 | 1 | <!--{
|
2 | 2 | "Title": "The Go Programming Language Specification",
|
3 |
| - "Subtitle": "Version of Dec 27, 2023", |
| 3 | + "Subtitle": "Language version go1.22 (Jan 30, 2023)", |
4 | 4 | "Path": "/ref/spec"
|
5 | 5 | }-->
|
6 | 6 |
|
@@ -6661,7 +6661,7 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
|
6661 | 6661 | string s string type index i int see below rune
|
6662 | 6662 | map m map[K]V key k K m[k] V
|
6663 | 6663 | channel c chan E, <-chan E element e E
|
6664 |
| -integer n integer type I value i I |
| 6664 | +integer n integer type value i see below |
6665 | 6665 | </pre>
|
6666 | 6666 |
|
6667 | 6667 | <ol>
|
@@ -6703,26 +6703,33 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
|
6703 | 6703 |
|
6704 | 6704 | <li>
|
6705 | 6705 | For an integer value <code>n</code>, the iteration values 0 through <code>n-1</code>
|
6706 |
| -are produced in increasing order, with the same type as <code>n</code>. |
| 6706 | +are produced in increasing order. |
6707 | 6707 | If <code>n</code> <= 0, the loop does not run any iterations.
|
6708 | 6708 | </li>
|
6709 | 6709 | </ol>
|
6710 | 6710 |
|
6711 |
| -<p> |
6712 |
| -The iteration values are assigned to the respective |
6713 |
| -iteration variables as in an <a href="#Assignment_statements">assignment statement</a>. |
6714 |
| -</p> |
6715 |
| - |
6716 | 6711 | <p>
|
6717 | 6712 | The iteration variables may be declared by the "range" clause using a form of
|
6718 | 6713 | <a href="#Short_variable_declarations">short variable declaration</a>
|
6719 | 6714 | (<code>:=</code>).
|
6720 |
| -In this case their types are set to the types of the respective iteration values |
6721 |
| -and their <a href="#Declarations_and_scope">scope</a> is the block of the "for" statement; |
6722 |
| -each iteration has its own separate variables [<a href="#Go_1.22">Go 1.22</a>] |
| 6715 | +In this case their <a href="#Declarations_and_scope">scope</a> is the block of the "for" statement |
| 6716 | +and each iteration has its own new variables [<a href="#Go_1.22">Go 1.22</a>] |
6723 | 6717 | (see also <a href="#For_clause">"for" statements with a ForClause</a>).
|
6724 |
| -If the iteration variables are declared outside the “for” statement, |
6725 |
| -after execution their values will be those of the last iteration. |
| 6718 | +If the range expression is a (possibly untyped) integer expression <code>n</code>, |
| 6719 | +the variable has the same type as if it was |
| 6720 | +<a href="#Variable_declarations">declared</a> with initialization |
| 6721 | +expression <code>n</code>. |
| 6722 | +Otherwise, the variables have the types of their respective iteration values. |
| 6723 | +</p> |
| 6724 | + |
| 6725 | +<p> |
| 6726 | +If the iteration variables are not explicitly declared by the "range" clause, |
| 6727 | +they must be preexisting. |
| 6728 | +In this case, the iteration values are assigned to the respective variables |
| 6729 | +as in an <a href="#Assignment_statements">assignment statement</a>. |
| 6730 | +If the range expression is a (possibly untyped) integer expression <code>n</code>, |
| 6731 | +<code>n</code> too must be <a href="#Assignability">assignable</a> to the iteration variable; |
| 6732 | +if there is no iteration variable, <code>n</code> must be assignable to <code>int</code>. |
6726 | 6733 | </p>
|
6727 | 6734 |
|
6728 | 6735 | <pre>
|
@@ -6765,6 +6772,11 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
|
6765 | 6772 | // type of i is int (default type for untyped constant 10)
|
6766 | 6773 | f(i)
|
6767 | 6774 | }
|
| 6775 | + |
| 6776 | +// invalid: 256 cannot be assigned to uint8 |
| 6777 | +var u uint8 |
| 6778 | +for u = range 256 { |
| 6779 | +} |
6768 | 6780 | </pre>
|
6769 | 6781 |
|
6770 | 6782 |
|
|
0 commit comments