Skip to content

Commit 26ba75f

Browse files
griesemergopherbot
authored andcommitted
doc: document new iteration variable semantics in spec
For #56010. Change-Id: Icca987a03d80587dd0e901f596ff7788584893ed Reviewed-on: https://go-review.googlesource.com/c/go/+/551095 Reviewed-by: Matthew Dempsky <[email protected]> TryBot-Bypass: Robert Griesemer <[email protected]> Reviewed-by: Rob Pike <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]>
1 parent 1dddd83 commit 26ba75f

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

doc/go_spec.html

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6541,7 +6541,6 @@ <h4 id="For_clause">For statements with <code>for</code> clause</h4>
65416541
and a <i>post</i> statement, such as an assignment,
65426542
an increment or decrement statement. The init statement may be a
65436543
<a href="#Short_variable_declarations">short variable declaration</a>, but the post statement must not.
6544-
Variables declared by the init statement are re-used in each iteration.
65456544
</p>
65466545

65476546
<pre class="ebnf">
@@ -6573,6 +6572,48 @@ <h4 id="For_clause">For statements with <code>for</code> clause</h4>
65736572
for { S() } is the same as for true { S() }
65746573
</pre>
65756574

6575+
<p>
6576+
Each iteration has its own separate declared variable (or variables)
6577+
[<a href="#Go_1.22">Go 1.22</a>].
6578+
The variable used by the first iteration is declared by the init statement.
6579+
The variable used by each subsequent iteration is declared implicitly before
6580+
executing the post statement and initialized to the value of the previous
6581+
iteration's variable at that moment.
6582+
</p>
6583+
6584+
<pre>
6585+
var prints []func()
6586+
for i := 0; i < 5; i++ {
6587+
prints = append(prints, func() { println(i) })
6588+
i++
6589+
}
6590+
for _, p := range prints {
6591+
p()
6592+
}
6593+
</pre>
6594+
6595+
<p>
6596+
prints
6597+
</p>
6598+
6599+
<pre>
6600+
0
6601+
3
6602+
5
6603+
</pre>
6604+
6605+
<p>
6606+
Prior to [<a href="#Go_1.22">Go 1.22</a>], iterations share one set of variables
6607+
instead of having their own separate variables.
6608+
In that case, the example above prints
6609+
</p>
6610+
6611+
<pre>
6612+
6
6613+
6
6614+
6
6615+
</pre>
6616+
65766617
<h4 id="For_range">For statements with <code>range</code> clause</h4>
65776618

65786619
<p>
@@ -6677,9 +6718,10 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
66776718
<a href="#Short_variable_declarations">short variable declaration</a>
66786719
(<code>:=</code>).
66796720
In this case their types are set to the types of the respective iteration values
6680-
and their <a href="#Declarations_and_scope">scope</a> is the block of the "for"
6681-
statement; they are re-used in each iteration.
6682-
If the iteration variables are declared outside the "for" statement,
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>]
6723+
(see also <a href="#For_clause">"for" statements with a ForClause</a>).
6724+
If the iteration variables are declared outside the “for” statement,
66836725
after execution their values will be those of the last iteration.
66846726
</p>
66856727

@@ -8550,7 +8592,11 @@ <h4 id="Go_1.21">Go 1.21</h4>
85508592
<h4 id="Go_1.22">Go 1.22</h4>
85518593
<ul>
85528594
<li>
8553-
A <a href="#For_range">"for" statement with a "range" clause</a> may iterate over
8595+
In a <a href="#For_statements">"for" statement</a>, each iteration has its own set of iteration
8596+
variables rather than sharing the same variables in each iteration.
8597+
</li>
8598+
<li>
8599+
A "for" statement with <a href="#For_range">"range" clause</a> may iterate over
85548600
integer values from zero to an upper limit.
85558601
</li>
85568602
</ul>

0 commit comments

Comments
 (0)