Go 1 22

Overview

Go 1.22, released in February 2024, addresses long-standing issues and modernizes key standard library packages. The release fixes a decade-old loop variable scoping problem, introduces powerful HTTP routing patterns, and provides a modern random number generation API.

Key Features

For Loop Per-Iteration Variables

Fixes closure bugs by creating new variables per iteration:

// Go 1.22: Each goroutine sees its own value
for i := 0; i < 3; i++ {
    go func() {
        fmt.Println(i)  // Correct: 0, 1, 2 (or permutation)
    }()
}

Before Go 1.22, all goroutines would print 3.

Range Over Integers

Simplified integer iteration:

// Go 1.22: Range over integer
for i := range 5 {
    fmt.Println(i)  // 0, 1, 2, 3, 4
}

// Repeat N times
for range 10 {
    doSomething()
}

Enhanced HTTP Routing

Method-specific handlers and path wildcards:

mux := http.NewServeMux()

// Method restrictions
mux.HandleFunc("GET /users", listUsers)
mux.HandleFunc("POST /users", createUser)

// Path wildcards
mux.HandleFunc("GET /users/{id}", getUser)
mux.HandleFunc("GET /files/{path...}", serveFile)

// Extract path values
func getUser(w http.ResponseWriter, r *http.Request) {
    id := r.PathValue("id")  // No parsing needed
    // ...
}

math/rand/v2 Package

Modernized random number API:

import "math/rand/v2"

// Automatically seeded (no manual seed needed)
n := rand.IntN(100)  // 0 to 99
f := rand.Float64()  // 0.0 to 1.0

// Idiomatic naming: IntN instead of Intn

References


Last Updated: 2026-02-04 Go Version: 1.22+ (recommended), 1.25.x (latest stable)

Last updated