1
1
<!--{
2
2
"Title": "The Go Programming Language Specification",
3
- "Subtitle": "Language version go1.22 (April 24 , 2024)",
3
+ "Subtitle": "Language version go1.22 (April 25 , 2024)",
4
4
"Path": "/ref/spec"
5
5
}-->
6
6
@@ -6656,13 +6656,13 @@ <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 n integer type 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
6666
</ pre >
6667
6667
6668
6668
< ol >
@@ -6703,8 +6703,17 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
6703
6703
</ li >
6704
6704
6705
6705
< li >
6706
- For an integer value < code > n</ code > , the iteration values 0 through < code > n-1</ code >
6706
+ For an integer value < code > n</ code > , where < code > n</ code > is of < a href ="#Numeric_types "> integer type</ a >
6707
+ or an untyped < a href ="#Constants "> integer constant</ a > , the iteration values 0 through < code > n-1</ code >
6707
6708
are produced in increasing order.
6709
+ If < code > n</ code > is of integer type, the iteration values have that same type.
6710
+ Otherwise, the type of < code > n</ code > is determined as if it were assigned to the
6711
+ iteration variable.
6712
+ Specifically:
6713
+ if the iteration variable is preexisting, the type of the iteration values is the type of the iteration
6714
+ variable, which must be of integer type.
6715
+ Otherwise, if the iteration variable is declared by the "range" clause or is absent,
6716
+ the type of the iteration values is the < a href ="#Constants "> default type</ a > for < code > n</ code > .
6708
6717
If < code > n</ code > <= 0, the loop does not run any iterations.
6709
6718
</ li >
6710
6719
</ ol >
@@ -6716,21 +6725,14 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
6716
6725
In this case their < a href ="#Declarations_and_scope "> scope</ a > is the block of the "for" statement
6717
6726
and each iteration has its own new variables [< a href ="#Go_1.22 "> Go 1.22</ a > ]
6718
6727
(see also < a href ="#For_clause "> "for" statements with a ForClause</ a > ).
6719
- If the range expression is a (possibly untyped) integer expression < code > n</ code > ,
6720
- the variable has the same type as if it was
6721
- < a href ="#Variable_declarations "> declared</ a > with initialization
6722
- expression < code > n</ code > .
6723
- Otherwise, the variables have the types of their respective iteration values.
6728
+ The variables have the types of their respective iteration values.
6724
6729
</ p >
6725
6730
6726
6731
< p >
6727
6732
If the iteration variables are not explicitly declared by the "range" clause,
6728
6733
they must be preexisting.
6729
6734
In this case, the iteration values are assigned to the respective variables
6730
6735
as in an < a href ="#Assignment_statements "> assignment statement</ a > .
6731
- If the range expression is a (possibly untyped) integer expression < code > n</ code > ,
6732
- < code > n</ code > too must be < a href ="#Assignability "> assignable</ a > to the iteration variable;
6733
- if there is no iteration variable, < code > n</ code > must be assignable to < code > int</ code > .
6734
6736
</ p >
6735
6737
6736
6738
< pre >
@@ -6778,6 +6780,10 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
6778
6780
var u uint8
6779
6781
for u = range 256 {
6780
6782
}
6783
+
6784
+ // invalid: 1e3 is a floating-point constant
6785
+ for range 1e3 {
6786
+ }
6781
6787
</ pre >
6782
6788
6783
6789
0 commit comments