What Is New in Go 1.22
| Category | Key Changes |
|---|---|
| Language | Range-over-function experiments, range-over-integer support |
| Standard Library | Enhanced HTTP routing, new math/rand/v2 package, database/sql improvements |
| Tooling | Improved loop variable capture, better program counter alignment |
| Performance | Faster stack growth, optimized compiler, PGO enhancements |
| Security | More secure randomness, vet improvements for loops and time formats |
What language changes should I know about?
Go 1.22 introduces range-over-integer support, letting you write cleaner loops. Instead of the traditional for i := 0; i < 10; i++, you can now use for i := range 10 for simpler iteration.
The most significant change affects loop variable capture. Previously, loop variables had a single address shared across iterations, causing bugs in goroutines. Now, each iteration gets its own variable, making concurrent loops much safer.
There's also an experimental range-over-functions feature, but this requires enabling with GOEXPERIMENT=rangefunc. This explores new ways to iterate but isn't production-ready yet.
How does HTTP routing improve in 1.22?
The net/http package now supports enhanced pattern matching in ServeMux. You can use methods like POST /items/{id} and wildcards like /files/{path...} directly without third-party routers.
This means you can write RESTful APIs with path parameters natively. The router now matches methods specifically and validates patterns at registration time, catching errors early.
In practice, this reduces dependency on external routing libraries for many common use cases. The built-in router now handles most basic API routing needs effectively.
What's new with math/rand/v2?
Go 1.22 introduces math/rand/v2 with cleaner APIs and better algorithms. The new package fixes several design warts from the original, like unclear method names and inconsistent behavior.
It uses a higher-quality random number generator by default and provides new methods like N for generating random integers within a range. The original math/rand remains available for backward compatibility.
This matters because the v2 package offers both performance improvements and more predictable behavior. For new code, you should prefer the v2 package whenever possible.
What tooling improvements help developers?
The go vet tool now detects more potential bugs, including invalid time formats in time.Format calls. It also checks for accidental shadowing of loop variables before the new semantics.
Profile-guided optimization (PGO) can now handle larger profiles and delivers better optimizations. The compiler uses PGO information more effectively to inline functions and optimize code paths.
Stack growth has been optimized to reduce overhead, particularly for small stacks. This improves performance in goroutine-heavy applications without changing your code.
How does database/sql get better?
The database/sql package now supports named parameters and retries for connection attempts. You can use :name placeholders instead of just ? in your SQL queries.
Connection pool handling is more robust with better error recovery. The package will automatically retry failed connection attempts when appropriate, making applications more resilient.
These changes simplify working with complex queries and improve reliability in distributed database environments. The named parameters especially help with readability of SQL code.
FAQ
Will the loop variable change break my existing code?
Yes, but intentionally. The change fixes a common bug where goroutines in loops would capture the same variable. Your code might behave differently now, but more correctly. The old behavior was considered buggy.
Should I use math/rand or math/rand/v2 for new projects?
Use math/rand/v2 for new code. It has better APIs, improved performance, and more predictable behavior. The original package remains for compatibility but won't receive new features.
Can I use the new HTTP routing patterns with existing code?
Yes, the enhanced ServeMux is backward compatible. Existing patterns continue to work, but you can gradually adopt the new method-specific and wildcard patterns in your routes.
How do I try the range-over-functions experiment?
Set GOEXPERIMENT=rangefunc in your environment before building. Remember this is experimental and might change or be removed in future versions, so don't use it in production.
Does the database/sql named parameter support work with all drivers?
No, driver support is required. The feature works with drivers that implement the new NamedValueChecker interface. Check your driver's documentation for compatibility.