Standard library / net/http
http.ServeMux.ServeHTTP()
Dispatches an HTTP request to the selected handler, setting path values for wildcard patterns. Expected stdout: '7\n'.
Formal Syntax & Structure
go syntaxgo
func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| w | ResponseWriter | Destination writer. |
| r | *Request | Source reader. |
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")) }) w := httptest.NewRecorder() m.ServeHTTP(w, httptest.NewRequest("GET", "/items/7", nil)) fmt.Println(w.Body.String())}
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) }}