Go 1.22 Personal Top Features
Exploring Go 1.22’s Best Upgrades
One of the most anticipated events for the Go developers is the new version release of the Go. Go 1.22 was released on February 6, 2024. I still like the way Go evolves keeping language simple to learn and use. In this post, I want to review some Go features that appeared in version 1.22.
“For” loops changes
The only programming language change this time is a change to the “for” loops. But here we received two improvements. The first is that the variable declared in the “for” loop created each new iteration. It avoids a lot of bugs related to sharing variables.
Let’s review a very simple example:
package main
import (
"fmt"
"sync"
"time"
)
func main() {
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
time.Sleep(1 * time.Second)
fmt.Println(i)
wg.Done()
}()
}
wg.Wait()
}
While running this code with Go 1.21 you will receive the next output:
10
10
10
10
10
10
10
10
10
10
It happens because all goroutines use the same variable i
and its value is evaluated only while the goroutine code is executed.
With Go 1.22 this is not the issue anymore because each new iteration variable i
is redeclared. So output you’ll receive will be something like this:
0
5
9
8
7
6
3
4
2
1
This new feature helps to avoid bugs related to concurrent execution and memory sharing. This perfectly fits in Go’s philosophy.
Another new feature is the range over integers. The previous example can be simplified with Go 1.22:
package main
import (
"fmt"
"sync"
"time"
)
func main() {
var wg sync.WaitGroup
wg.Add(10)
for i := range 10 {
go func() {
time.Sleep(1 * time.Second)
fmt.Println(i)
wg.Done()
}()
}
wg.Wait()
}
This code will iterate from 0 to 9 the same as the previous code. This feature is “syntax sugar”. Construction for i := 0; i < N; i++
is used often by developers so it will save some time by simplifying it to for i := range N
.
New math/rand/v2 package
It is the first V2 package in the Go’s core library! I’m 100% sure you’ve used math/rand
package before. So this library received new changes that were implemented in the math/rand/v2
package. The global functions now are randomly seeded so some functions (like Int32
, etc.) can be used without creating a random generator.
The new global function N
can be used with any integer type. You can even do something like this:
package main
import (
"fmt"
"math/rand/v2"
"time"
)
func main() {
randWait := rand.N(5 * time.Second)
fmt.Println("Sleeping for", randWait)
time.Sleep(randWait)
fmt.Println("Woke up!")
}
It will generate a random duration from 0 to 5 seconds and sleep for that time.
New go/version package
Probably not the package I’m going to use, but still now you can compare and check Go versions with the new go/version
package:
package main
import (
"fmt"
"go/version"
)
func main() {
fmt.Printf("Versions 'go1.21' and 'go1.22' are comparable: %v\n", version.Compare("go1.21", "go1.22"))
fmt.Printf("Version 'go1.21' is valid: %v\n", version.IsValid("go1.21"))
}
The output is next:
Versions 'go1.21' and 'go1.22' are comparable: -1
Version 'go1.21' is valid: true
Conclusion
You can find more features, improvements and details here:
This version does not have a lot of programming language features, but still a lot of was improved in the core libraries and Go tools. Hope Go new versions will proceed moving this way, continuously improving performance, simplicity, and efficiency, while also embracing modern programming needs and community feedback.
Support Me
If you found joy or value in my article, and you’d like to show some appreciation, you can now donate with ‘Buy Me A Coffee’! Your support will go a long way in helping me continue to share more stories, insights, and content that resonates with you.