Go 1.22 `net/http` routing: use method and path patterns with `PathValue`
et/http routing: use method and path patterns with PathValue
et/httprouting: use method and path patterns withPathValue`
Since Go 1.22, the standard net/http.ServeMux supports patterns that combine an HTTP method and path wildcard, such as GET /tasks/{id}. A handler reads the wildcard with r.PathValue("id"). If a path matches but the request uses an unregistered method, the mux returns 405 Method Not Allowed. This guide uses Go 1.22+ and the standard library to build a minimal API.
Check the version and matching rules
Run go version and check both the module's go directive and the deployed Go version. ServeMux pattern syntax and matching changed in Go 1.22. An application that needs the old behavior can set GODEBUG=httpmuxgo121=1 at startup, but code using the new wildcards should be reviewed against current rules.
package main
import (
"fmt"
"net/http"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("GET /tasks/{id}", func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
fmt.Fprintf(w, "task %s", id)
})
mux.HandleFunc("POST /tasks", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, "created")
})
if err := http.ListenAndServe(":8080", mux); err != nil {
panic(err)
}
}Put the method and path in the pattern
GET /tasks/{id} matches a GET request with one path segment after /tasks/; the handler reads its value with Request.PathValue. A GET pattern also matches HEAD; other methods must match exactly. If a path matches but has no pattern for the requested method, the default mux can return 405 instead of requiring one handler to manually branch on every method.
When several patterns match, the more specific one wins. Conflicting patterns where neither is more specific can panic during registration. Wildcards must occupy whole path segments. A terminal wildcard such as {rest...} captures the remaining path, including slashes. Review broad prefix patterns against concrete routes.
Distinguish 404 from 405
No matching path pattern produces 404. A matching path with no matching method produces 405 and an Allow header listing supported methods. For 404, inspect the path pattern and segment count. For 405, inspect the method token and registered methods.
Verify locally
Save the file as main.go and run go run . with Go 1.22+. curl -i http://localhost:8080/tasks/42 should return 200 and task 42; curl -i -X POST http://localhost:8080/tasks should return 201 and created. DELETE /tasks/42 should return 405 with Allow; /unknown should return 404. These are expected results; the code was not run here.
If the program panics while registering patterns, compare the conflicting patterns and remove ambiguous rules. On Go 1.21 or earlier, wildcard and registration behavior differs; confirm the binary uses the intended Go version. This is not a comparison of third-party routers.
Official sources
[Go 1.22 routing enhancements](https://go.dev/blog/routing-enhancements)
[`net/http` package documentation](https://pkg.go.dev/net/http)
[Go 1.22 ServeMux source and compatibility notes](https://go.dev/src/net/http/server.go)