---
source: https://iinventedlight.com/library/notes/go-http-servemux-patterns.md
title: "Go 1.22 ServeMux patterns match methods and {wildcards}; read them with r.PathValue"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Go standard library docs (pkg.go.dev)); CC-BY-4.0 (The Go Blog); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Go standard library docs (pkg.go.dev), net/http package; The Go Blog, Routing Enhancements for Go 1.22. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. CC-BY-4.0 (https://go.dev/copyright). Copyright The Go Authors."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: Go net/http ServeMux routing"
tags: [go, net/http, ServeMux, routing, PathValue, wildcards]
client: Librarian
sources:
  - id: S1
    title: "net/http package"
    publisher: Go standard library docs (pkg.go.dev)
    url: https://pkg.go.dev/net/http
    accessed: 2026-11-18
    license: "BSD-3-Clause"
  - id: S2
    title: "Routing Enhancements for Go 1.22"
    publisher: The Go Blog
    url: https://go.dev/blog/routing-enhancements
    accessed: 2026-11-18
    license: "CC-BY-4.0"
---
# Go 1.22 ServeMux patterns match methods and {wildcards}; read them with r.PathValue
**Answer:** Since Go 1.22, `http.ServeMux` patterns can match the method, host and path of a request, for example `"GET /posts/{id}"`. Extract the identifier with the new `PathValue` method on Request. When two patterns overlap, the most specific pattern wins.

## Example

```go
mux := http.NewServeMux()
mux.HandleFunc("GET /posts/{id}", func(w http.ResponseWriter, req *http.Request) {
    id := req.PathValue("id")
    fmt.Fprintf(w, "post %s", id)
})
mux.HandleFunc("GET /posts/latest", latest)
mux.HandleFunc("/files/{pathname...}", files)
mux.HandleFunc("/posts/{$}", index)
log.Fatal(http.ListenAndServe(":8080", mux))
```

## Pattern syntax

- A pattern can name the HTTP method. As a special case, `GET` also matches `HEAD`; all the other methods match exactly.
- `{name}` matches one path segment; a wildcard ending in `...`, as in `/files/{pathname...}`, can match all the remaining segments of the path.
- A pattern like `/posts/` that ends in a slash matches all paths beginning with that string. Write `/posts/{$}` to match only the path with the trailing slash.

## Precedence

One pattern is more specific than another if it matches a strict subset of requests. `/posts/latest` is more specific than `/posts/{id}`, so it wins for that path.

## Scope of the change

The new features almost exclusively affect the pattern string passed to `Handle` and `HandleFunc`. The only API changes are two new methods on `Request` for wildcard matches: `PathValue` and `SetPathValue`.

## See also

- [go-context](https://iinventedlight.com/library/notes/go-context/)
- [go-error-handling](https://iinventedlight.com/library/notes/go-error-handling/)
- [go-structured-logging-slog](https://iinventedlight.com/library/notes/go-structured-logging-slog/)

## Claims

- id: C1
  claim: Patterns can match method, host and path.
  source: S1
  evidence: "Patterns can match the method, host and path of a request."

- id: C2
  claim: GET also matches HEAD.
  source: S2
  evidence: "As a special case, GET also matches HEAD"

- id: C3
  claim: The most specific pattern wins.
  source: S2
  evidence: "The precedence rule is simple: the most specific pattern wins."

- id: C4
  claim: /posts/{$} matches only the path with the trailing slash.
  source: S2
  evidence: "To match only the path with the trailing slash, you can write /posts/{$}"

## Sources

- [S1] net/http package. Go standard library docs (pkg.go.dev). https://pkg.go.dev/net/http . License: BSD-3-Clause. Accessed 2026-11-18.

- [S2] Routing Enhancements for Go 1.22. The Go Blog. https://go.dev/blog/routing-enhancements . License: CC-BY-4.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go standard library docs (pkg.go.dev), net/http package; The Go Blog, Routing Enhancements for Go 1.22. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. CC-BY-4.0 (https://go.dev/copyright). Copyright The Go Authors. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
