Go 1.22
Version on2024Year2moon6Release on Sunday, introducing several important features and improvements. On the language level, this versionfor
The loop has two updates:
- Each iteration of the loop creates new variables
- Loop supports iterating on integer ranges
This article will be correctfor
Two updates of the loop are introduced.
Ready? Prepare a cup of your favorite coffee or tea and find out with this article.
Each iteration of the loop creates new variables
existGo 1.22
Before the version,for
The variable declared by the loop is created only once and updated in each iteration. existGo 1.22
In this, each iteration of the loop creates new variables, which will prevent unexpected sharing errors.
By the same code examples in differentGo
Running in the version, we can clearly see the difference between the running results, and thus feel the specific impact of language updates.
Go 1.21
Version of the code example
package main import "fmt" func main() { done := make(chan bool) values := []string{"chen", "ming", "yong"} for _, v := range values { go func() { (v) done <- true }() } // Wait until all goroutines are executed for _ = range values { <-done } }
The code run results are as follows:
yong
yong
yong
exist
for
Anonymous functions and loop variables in a loop bodyv
A closure is formed. Closure capturedv
Reference (or address) of this causes all closure instances to be accessedv
In fact, the same memory address is accessed. becausev
Created only once and will be updated with each iteration. Therefore, the final print results are the same valueyong
。Go 1.22
Version code example
package main import "fmt" func main() { done := make(chan bool) values := []string{"chen", "ming", "yong"} for _, v := range values { go func() { (v) done <- true }() } // Wait until all goroutines are executed for _ = range values { <-done } }
The code run results are as follows:
yong
ming
chen
-
exist
Go 1.22
In, each iteration of the loop creates a new variablev
, which means that each closure holds a different onev
References to variables, so the final print results will vary.Each iteration of the loop creates new variablesThis update effectively avoids common closure traps in previous versions and improves the security and predictability of the code.
Loop supports iterating on integer ranges
existGo 1.22
Before the version, when we usefor range
When only supportedarray or slice
、string
、map
andchannel
The expression of type is iterated, andGo 1.22
Starting from the version, additional new pairsinterger
Support for types, which means we can use integers directly for loop iteration.
package main import "fmt" func main() { for i := range 10 { (i) } }
For integer values10
, iterate the value from0
arrive9
Generate in incremental order. ifrange
The following expression is0
, then the loop does not perform any iteration.
summary
This article introducesGo 1.22
Versionfor
Two important updates made by the loop:Each iteration of the loop creates new variablesandLoop supports iterating on integer ranges。
Each iteration of the loop creates new variables, effectively avoids common closure traps in previous versions, and improves the security and predictability of the code.
Loop supports iterating on integer ranges, greatly enhancedfor
The flexibility of loops makes writing count loops more direct and concise.
This is the end of this article about the two important updates of Go 1.22 for loop. For more relevant content on Go 1.22 for loop updates, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!