Go 1.22 ServeMux Routing: When the Standard Library Is Enough
Go 1.22 made the standard library’s HTTP ServeMux more expressive. Patterns can include methods and wildcards, and a handler can read a matched path segment from the request. For small services, this may remove a router dependency and leave a familiar net/http foundation.
A pattern can describe a method and path, such as POST /items/{id}. A wildcard captures one path segment; a trailing multi-segment wildcard handles the remainder. More specific patterns take precedence over broader ones, and conflicting registrations can be rejected. That is enough for many JSON APIs with a modest set of resources.
Before replacing an established router, compare the surrounding features rather than only route syntax. Middleware composition, route groups, parameter constraints, generated API documentation, and test helpers may matter more than one less dependency. If the existing router is stable and provides capabilities the application uses, migration may create work without simplifying the system.
Compatibility also deserves a deliberate look. The new semantics depend on the Go version in the module, and older pattern registrations may now interact differently with more specific routes. Add tests for method mismatches, trailing slashes, wildcard values, and overlapping paths before adopting the new patterns.
This is a useful standard-library improvement, not a claim that every Go server should remove its router. Choose the smallest routing model that stays clear as the API grows.
The Go 1.22 release notes describe method patterns and wildcards. The Go team’s routing enhancements article gives examples of precedence and path values.