Routing and Path Parameters
Registering handlers and parsing URL params
Routing and Path Parameters is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Standard mux limitations
The default http.ServeMux does not support path parameters (e.g. /users/{id}) or method-based routing. Go 1.22 added enhanced pattern support.
Go 1.22 enhanced patterns
From Go 1.22, ServeMux supports method prefix and path parameters:
mux.HandleFunc("GET /users/{id}", func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
fmt.Fprintln(w, "user:", id)
})r.PathValue
r.PathValue("name") extracts a named segment from the URL pattern (Go 1.22+).
// Pattern: "GET /products/{category}/{id}"
category := r.PathValue("category")
id := r.PathValue("id")Manual path parsing
For Go < 1.22 or complex rules, extract path segments with strings.Split(r.URL.Path, "/"):
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
// /users/42 → ["users", "42"]Method routing
Use a switch on r.Method for per-method logic within a single handler:
switch r.Method {
case http.MethodGet:
getHandler(w, r)
case http.MethodPost:
createHandler(w, r)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}Popular third-party routers
Libraries like gorilla/mux, chi, and httprouter provide richer routing: regex constraints, named groups, middleware chains, and subrouters.
chi router
chi is a lightweight, idiomatic router compatible with net/http handlers:
r := chi.NewRouter()
r.Get("/users/{id}", getUser)
http.ListenAndServe(":8080", r)Query parameters
Read query parameters from r.URL.Query() which returns a url.Values map:
page := r.URL.Query().Get("page")
if page == "" { page = "1" }Wildcard and catch-all
In Go 1.22 patterns, {name...} is a wildcard that matches the rest of the path:
mux.HandleFunc("/static/{path...}", serveStatic)Nested routes
Group related routes with a subrouter or by registering handlers with a common prefix. chi's r.Route creates a subrouter for a prefix.
Trailing slash redirect
http.ServeMux automatically redirects /path/ to /path or vice versa. To disable, strip trailing slashes in a middleware before the mux receives the request.
Quick Check
In Go 1.22+, how do you extract the {id} segment from a matched URL pattern?
Recap: Routing and Path Parameters
Key points:
- Go 1.22: method prefix + {name} path parameters in ServeMux
- r.PathValue("name") extracts named segments
- Method switch for per-method logic
- Third-party routers (chi, gorilla/mux) for complex routing
Frequently asked questions
Is the “Routing and Path Parameters” lesson free?
Yes — the full text of “Routing and Path Parameters” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Routing and Path Parameters”?
Registering handlers and parsing URL params You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Routing and Path Parameters” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating an HTTP Server
- Routing and Path Parameters
- Middleware Pattern
- Graceful Shutdown