From fe1b2f95e6dbfb6e6212bb391706ae62eb0ae5ec Mon Sep 17 00:00:00 2001 From: David Chase Date: Wed, 29 Nov 2023 16:09:26 -0500 Subject: [PATCH] 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 Run-TryBot: David Chase TryBot-Result: Gopher Robot Reviewed-by: Alan Donovan --- doc/go1.22.html | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/doc/go1.22.html b/doc/go1.22.html index c87d9d3b21..7e2b4da20c 100644 --- a/doc/go1.22.html +++ b/doc/go1.22.html @@ -26,7 +26,39 @@ Do not send CLs removing the interior tags from such phrases.

Changes to the language

- TODO: complete this section + +Go 1.22 makes two changes to "for" loops. +

    +
  • + 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 transition support tooling + described in the proposal continues to work in the same way it did in Go 1.21. +
  • +
  • + "For" loops may now range over integers. + For example: +
    +package main
    +
    +import "fmt"
    +
    +func main() {
    +  for i := range 10 {
    +    fmt.Println(10 - i)
    +  }
    +  fmt.Println("go1.22 has lift-off!")
    +}
    +
    + See the spec for details. +
  • +
+ + +

+

+ Go 1.22 includes a preview of a language change we are considering + for a future version of Go: range-over-function iterators. + Building with GOEXPERIMENT=rangefunc enables this feature.

Tools

-- 2.50.0