Quick Guide: Go 1.22 Features

Emre Odabas
3 min readJan 31, 2024

Go 1.21 was released nearly six months ago, and the community has already released rc3 of Go 1.22. If you wonder what comes with earlier versions, you could check For Quick Guide: Go 1.21 Features

Go 1.22 will be released in February 2024. This version mainly focused on range-over functions and some developer experiences. Let’s look at what comes with Go 1.22.

https://www.quasilyte.dev/gopherkon/?state=0d080d0d03000900000200000000000008

Table of Contents

“For” Loop Variables

As you know, a variable by a “for” loop is created once and updated by each iteration. This behavior has caused several bugs so far when you share this variable. The strategy will be created for each iteration. Let's look at a sample case. You can check this code behavior on the Go playgrounds.

This feature is also released as experimental with Go 1.21. If you wonder about the details, check this wiki.

^^^ go top ^^^

“For” Loop Range

Finally, we get an obvious feature on the range function via Go 1.22. We could write a simple loop with a range over integers.

 for i := range 3 {
fmt.Print(i)
}
// print 0 1 2

^^^ go top ^^^

Range Over Functions

This feature will be released as a preview on Go 1.22. You need to add the GOEXPERIMENT=rangefunc variable while building.

With this proposal, the Go team will add range-over function iterators. The main idea is to give generic iterator functionality for any data listing type. iter is the new package for these functions. Here is an example of slices you could define.

func main() {
s := []string{"hello", "world"}
for i, x := range Backward(s) {
fmt.Println(i, x)
}
}

func Backward[E any](s []E) func(func(int, E) bool) {
return func(yield func(int, E) bool) {
for i := len(s) - 1; i >= 0; i-- {
if !yield(i, s[i]) {
return
}
}
}
}
// 1 world
// 0 hello

Check this wiki page for the details.

^^^ go top ^^^

Random.N Function

With the power of the generic functions in Go, we have a new Random function that works with any integer type.

type intType interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

With this above intType , converting integers could be not an issue. Here is an example of this function.

type myInt int16

func randN() {
var x int64
var y int
var z uint
var w myInt
x, y, z, w = 100, 100, 100, 100
rand.N(x)
rand.N(y)
rand.N(z)
rand.N(w)
}

^^^ go top ^^^

Improvements

  • Profile Guided Optimization(PGO) could be more efficient via de-virtualizing a higher proportion of calls. This gives up to %14 performance when using PGO.
  • Type-based garbage collection improvements lead to %1–3 CPU performancex and reduce %1 memory overhead.
  • Enhancements on routing patterns including wildcard patterns like /items/{id} and /exact/match/{$}.
id:= Request.PathValue("id")
  • The new Null[T] type provides a way to scan nullable columns for any column types on database/sql package.
  • The slices package has a new Concat function.
var a = []string{"a", "b", "c"}
var x = []string{"x", "y", "z"}
concat := slices.Concat(a, x)
fmt.Println(concat)
// a b c x y z

^^^ go top ^^^

--

--