Skip to content

Commit

Permalink
doc: describe for loop changes (lifetime, range int; range func exper…
Browse files Browse the repository at this point in the history
…iment)

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 <cherryyz@google.com>
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
dr2chase committed Dec 8, 2023
1 parent f360ffd commit fe1b2f9
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion doc/go1.22.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,39 @@ <h2 id="introduction">DRAFT RELEASE NOTES — Introduction to Go 1.22</h2>
<h2 id="language">Changes to the language</h2>

<p>
TODO: complete this section
<!-- loop variable scope --><!-- range over int -->
Go 1.22 makes two changes to "for" loops.
<ul>
<li>
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.
The <a href="https://go.dev/wiki/LoopvarExperiment#my-test-fails-with-the-change-how-can-i-debug-it">transition support tooling</a>
described in the proposal continues to work in the same way it did in Go 1.21.
</li>
<li>
"For" loops may now range over integers.
For <a href="https://go.dev/play/p/ky02zZxgk_r?v=gotip">example</a>:
<pre>
package main

import "fmt"

func main() {
for i := range 10 {
fmt.Println(10 - i)
}
fmt.Println("go1.22 has lift-off!")
}
</pre>
See the spec for <a href="/ref/spec#For_range">details</a>.
</li>
</ul>

<!-- range over func GOEXPERIMENT; https://go.dev/issue/61405, https://go.dev/issue/61897, CLs 510541,539277,540263,543319 -->
</p>
<p>
Go 1.22 includes a preview of a language change we are considering
for a future version of Go: <a href="https://go.dev/wiki/RangefuncExperiment">range-over-function iterators</a>.
Building with <code>GOEXPERIMENT=rangefunc</code> enables this feature.
</p>

<h2 id="tools">Tools</h2>
Expand Down

0 comments on commit fe1b2f9

Please sign in to comment.