Standard library / net/http

http.ServeMux.Handler()

Returns the handler and matched pattern for the request without serving it. Use ServeHTTP for wildcard extraction. Expected stdout: 'GET /items/{id}\n'.

Formal Syntax & Structure

go syntaxgo
func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string)

Parameters & Attributes

NameTypeDescription
r*RequestSource reader.

Return Value

(h Handler, pattern string)

Handler selected by routing and the matched pattern; an empty pattern can indicate no match.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http"  "net/http/httptest")
func main() {  m := http.NewServeMux()  m.HandleFunc("GET /items/{id}", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, r.PathValue("id")) })  _, p := m.Handler(httptest.NewRequest("GET", "/items/42", nil))  fmt.Println(p)}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference