mirror of https://github.com/golang/go.git
spec: clarify iteration variable type for range over integer
Also: report language version (plus date) in spec header. Fixes #65137. Change-Id: I4f1d220d5922c40a36264df2d0a7bb7cd0756bac Reviewed-on: https://go-review.googlesource.com/c/go/+/557596 TryBot-Bypass: Robert Griesemer <gri@google.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
parent
c2dbbe4e94
commit
41d6687084
|
|
@ -1,6 +1,6 @@
|
||||||
<!--{
|
<!--{
|
||||||
"Title": "The Go Programming Language Specification",
|
"Title": "The Go Programming Language Specification",
|
||||||
"Subtitle": "Version of Dec 27, 2023",
|
"Subtitle": "Language version go1.22 (Jan 30, 2023)",
|
||||||
"Path": "/ref/spec"
|
"Path": "/ref/spec"
|
||||||
}-->
|
}-->
|
||||||
|
|
||||||
|
|
@ -6661,7 +6661,7 @@ array or slice a [n]E, *[n]E, or []E index i int a[i] E
|
||||||
string s string type index i int see below rune
|
string s string type index i int see below rune
|
||||||
map m map[K]V key k K m[k] V
|
map m map[K]V key k K m[k] V
|
||||||
channel c chan E, <-chan E element e E
|
channel c chan E, <-chan E element e E
|
||||||
integer n integer type I value i I
|
integer n integer type value i see below
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<ol>
|
<ol>
|
||||||
|
|
@ -6703,26 +6703,33 @@ is <code>nil</code>, the range expression blocks forever.
|
||||||
|
|
||||||
<li>
|
<li>
|
||||||
For an integer value <code>n</code>, the iteration values 0 through <code>n-1</code>
|
For an integer value <code>n</code>, the iteration values 0 through <code>n-1</code>
|
||||||
are produced in increasing order, with the same type as <code>n</code>.
|
are produced in increasing order.
|
||||||
If <code>n</code> <= 0, the loop does not run any iterations.
|
If <code>n</code> <= 0, the loop does not run any iterations.
|
||||||
</li>
|
</li>
|
||||||
</ol>
|
</ol>
|
||||||
|
|
||||||
<p>
|
|
||||||
The iteration values are assigned to the respective
|
|
||||||
iteration variables as in an <a href="#Assignment_statements">assignment statement</a>.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
The iteration variables may be declared by the "range" clause using a form of
|
The iteration variables may be declared by the "range" clause using a form of
|
||||||
<a href="#Short_variable_declarations">short variable declaration</a>
|
<a href="#Short_variable_declarations">short variable declaration</a>
|
||||||
(<code>:=</code>).
|
(<code>:=</code>).
|
||||||
In this case their types are set to the types of the respective iteration values
|
In this case their <a href="#Declarations_and_scope">scope</a> is the block of the "for" statement
|
||||||
and their <a href="#Declarations_and_scope">scope</a> is the block of the "for" statement;
|
and each iteration has its own new variables [<a href="#Go_1.22">Go 1.22</a>]
|
||||||
each iteration has its own separate variables [<a href="#Go_1.22">Go 1.22</a>]
|
|
||||||
(see also <a href="#For_clause">"for" statements with a ForClause</a>).
|
(see also <a href="#For_clause">"for" statements with a ForClause</a>).
|
||||||
If the iteration variables are declared outside the “for” statement,
|
If the range expression is a (possibly untyped) integer expression <code>n</code>,
|
||||||
after execution their values will be those of the last iteration.
|
the variable has the same type as if it was
|
||||||
|
<a href="#Variable_declarations">declared</a> with initialization
|
||||||
|
expression <code>n</code>.
|
||||||
|
Otherwise, the variables have the types of their respective iteration values.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
If the iteration variables are not explicitly declared by the "range" clause,
|
||||||
|
they must be preexisting.
|
||||||
|
In this case, the iteration values are assigned to the respective variables
|
||||||
|
as in an <a href="#Assignment_statements">assignment statement</a>.
|
||||||
|
If the range expression is a (possibly untyped) integer expression <code>n</code>,
|
||||||
|
<code>n</code> too must be <a href="#Assignability">assignable</a> to the iteration variable;
|
||||||
|
if there is no iteration variable, <code>n</code> must be assignable to <code>int</code>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
|
|
@ -6765,6 +6772,11 @@ for i := range 10 {
|
||||||
// type of i is int (default type for untyped constant 10)
|
// type of i is int (default type for untyped constant 10)
|
||||||
f(i)
|
f(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// invalid: 256 cannot be assigned to uint8
|
||||||
|
var u uint8
|
||||||
|
for u = range 256 {
|
||||||
|
}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue