Skip to content

Commit fe1b2f9

Browse files
committed
doc: describe for loop changes (lifetime, range int; range func experiment)
See also https://go.dev/wiki/RangefuncExperiment, written as a companion to this change. For #61422. Change-Id: I129bf38dd2fa4aef47454138b4ca5ed18653aecf Reviewed-on: https://go-review.googlesource.com/c/go/+/546095 Reviewed-by: Cherry Mui <[email protected]> Run-TryBot: David Chase <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent f360ffd commit fe1b2f9

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

doc/go1.22.html

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,39 @@ <h2 id="introduction">DRAFT RELEASE NOTES — Introduction to Go 1.22</h2>
2626
<h2 id="language">Changes to the language</h2>
2727

2828
<p>
29-
TODO: complete this section
29+
<!-- loop variable scope --><!-- range over int -->
30+
Go 1.22 makes two changes to "for" loops.
31+
<ul>
32+
<li>
33+
Previously, the variables declared by a "for" loop were created once and updated by each iteration. In Go 1.22, each iteration of the loop creates new variables, to avoid accidental sharing bugs.
34+
The <a href="https://go.dev/wiki/LoopvarExperiment#my-test-fails-with-the-change-how-can-i-debug-it">transition support tooling</a>
35+
described in the proposal continues to work in the same way it did in Go 1.21.
36+
</li>
37+
<li>
38+
"For" loops may now range over integers.
39+
For <a href="https://go.dev/play/p/ky02zZxgk_r?v=gotip">example</a>:
40+
<pre>
41+
package main
42+
43+
import "fmt"
44+
45+
func main() {
46+
for i := range 10 {
47+
fmt.Println(10 - i)
48+
}
49+
fmt.Println("go1.22 has lift-off!")
50+
}
51+
</pre>
52+
See the spec for <a href="/ref/spec#For_range">details</a>.
53+
</li>
54+
</ul>
55+
56+
<!-- range over func GOEXPERIMENT; https://go.dev/issue/61405, https://go.dev/issue/61897, CLs 510541,539277,540263,543319 -->
57+
</p>
58+
<p>
59+
Go 1.22 includes a preview of a language change we are considering
60+
for a future version of Go: <a href="https://go.dev/wiki/RangefuncExperiment">range-over-function iterators</a>.
61+
Building with <code>GOEXPERIMENT=rangefunc</code> enables this feature.
3062
</p>
3163

3264
<h2 id="tools">Tools</h2>

0 commit comments

Comments
 (0)