]> Cypherpunks.ru repositories - gostls13.git/commitdiff
doc: describe for loop changes (lifetime, range int; range func experiment)
authorDavid Chase <drchase@google.com>
Wed, 29 Nov 2023 21:09:26 +0000 (16:09 -0500)
committerDavid Chase <drchase@google.com>
Fri, 8 Dec 2023 23:05:48 +0000 (23:05 +0000)
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>
doc/go1.22.html

index c87d9d3b21e34598b71d0fd004cfd383d702d5a2..7e2b4da20c38044879b2b839f99499b5fa34b54a 100644 (file)
@@ -26,7 +26,39 @@ Do not send CLs removing the interior tags from such phrases.
 <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>