Go or golang was launched in 2009. Compiled programming language developed by google. It puts great emphasis on simplicity, efficiency, and concurrency. It hosts top notch features and is popular for web development, systems programming, networking, and command-line tools. Go is mostly adopted in cloud-native architectures, containerization, and data processing tasks. Overall, It is a favorite among developers for different application domains. They’ve recently released the latest version of Go, Version 1.22, It features exciting new features and updates which will be covered in this article.

Table of Contents

“For” loops changes

go
Image credit: Alternative to

In the latest version of Go, The only change in programming language that was observed is a change to the “for” loops. There were two main changes made. The first one being:-

  • The variable declared in the “for” loop created each new iteration.

A simple example would be:-

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()
}

In the older version, The output you’d get would be this

10
10
10
10
10
10
10
10
10
10

But in the new version of Go, The output is this:-

0
5
9
8
7
6
3
4
2
1

This new feature helps avoid bugs which perfectly fits into Go’s philosophy. Another new feature is:-

  • range over integers

The above example could be simplified by 1.22 into the following:-

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 new feature is called “syntax sugar”.

New math/rand/v2 package

It is the first v2 package that go has ever received. If you’re a developer then you must have used the math/rand package before. The new v2 package has changes like some global functions are now seeded so there is no need to randomly generate them anymore. The latest global function ‘N’ can now be utilized with any integer type. Other important changes in the new v2 package include:-

  1. The read method which is still available in math/rand was not carried forward to math/rand/v2.
  2. There is no source64 interface, it uses a single Uint64 method.
  3. Many methods now use faster algorithms that were not possible to adopt in math/rand.

An api migration tool is likely to be released in future versions like the 1.23

Enhanced routing patterns

To accept wildcards and methods, patterns used by net/http.ServeMux have been updated. A pattern with a method now takes precedence over a pattern without one and registering a handler with “GET” also registers it with “HEAD”. Wildcards in patterns, like /items/{id}, match segments of the URL path. The actual segment value may be accessed by calling the Request.PathValue method. A wildcard ending in “…”, like /files/{path...}, must occur at the end of a pattern and matches all the remaining segments. If you have overlapping patterns then the more specific one will be given preference and if both of them are the same then they conflict with one another.  This means that the order in which patterns are registered doesn’t matter. These changes break backwards compatibility in a few ways.

Changes in Go library

There have been minor changes in the library as well. You can read more about them here.

Read more from itechglam

Leave a Reply

Scroll to Top