@@ -6541,7 +6541,6 @@ <h4 id="For_clause">For statements with <code>for</code> clause</h4>
6541
6541
and a < i > post</ i > statement, such as an assignment,
6542
6542
an increment or decrement statement. The init statement may be a
6543
6543
< 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.
6545
6544
</ p >
6546
6545
6547
6546
< pre class ="ebnf ">
@@ -6573,6 +6572,48 @@ <h4 id="For_clause">For statements with <code>for</code> clause</h4>
6573
6572
for { S() } is the same as for true { S() }
6574
6573
</ pre >
6575
6574
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
+
6576
6617
< h4 id ="For_range "> For statements with < code > range</ code > clause</ h4 >
6577
6618
6578
6619
< p >
@@ -6677,9 +6718,10 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
6677
6718
< a href ="#Short_variable_declarations "> short variable declaration</ a >
6678
6719
(< code > :=</ code > ).
6679
6720
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,
6683
6725
after execution their values will be those of the last iteration.
6684
6726
</ p >
6685
6727
@@ -8550,7 +8592,11 @@ <h4 id="Go_1.21">Go 1.21</h4>
8550
8592
< h4 id ="Go_1.22 "> Go 1.22</ h4 >
8551
8593
< ul >
8552
8594
< 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
8554
8600
integer values from zero to an upper limit.
8555
8601
</ li >
8556
8602
</ ul >
0 commit comments