Http Handlefunc
Fast recall
Quick revision
Core answer
Whether you understand Http Handlefunc beyond memorized syntax.
Giving a definition without explaining a practical trade-off.
Connect the concept to a small production example and state its complexity or failure mode.
Interview-ready guide
Interview answer
The registration:
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello")
})
// GET /hello → 200 hello — verifiedThe two arguments: the pattern ("/hello" — exact path; "/" — catch-all prefix; "/static/" — subtree) and the handler function — a func(w http.ResponseWriter, r *http.Request).
The ResponseWriter (w): how you write the response — Fprintln/Fprintf writes the body, w.Header().Set() sets headers, w.WriteHeader(200) sets the status (implicit 200 if you write body first — write header BEFORE body).
The Request (r): everything about the incoming request — r.URL, r.Method, r.Header, r.Body, r.URL.Query(), r.Context(). Read the body via r.Body (io.ReadCloser — read and close).
The pattern rules: ServeMux matches longest-prefix — "/static/" (with trailing slash) is a subtree; "/exact" matches only that path; "/" matches everything (the catch-all). Go 1.22 added method+wildcard patterns: "GET /users/{id}" — the first significant routing upgrade.
The alternative — http.Handle: registers an http.Handler (an interface with ServeHTTP) instead of a func — the func version is a convenience adapter (http.HandlerFunc type).
Http Handlefunc
Direct Answer
Registers a handler function (func(w http.ResponseWriter, r *http.Request)) to a URL pattern on the DefaultServeMux (or a mux you create). The handler writes the response through w.
Deep Dive & Architecture
The ServeMux pattern matching, mechanically: a map from patterns to handlers, plus the routing decision on each request: exact match first (the map lookup), then longest-prefix among patterns with trailing slashes ("/static/" matches /static/x/y). The catch-all "/" is the default. Go 1.22 restructured this into a real router (method matching, path parameters like /users/{id}, and the {id} arrives in r.PathValue("id")) — the first time ServeMux competed with chi/gin for non-trivial routing.
The ResponseWriter's hidden state: it's an interface over the connection's buffered writer — headers are captured (Header().Set()) until the first Write or WriteHeader, then flushed. The consequence: setting headers AFTER writing body is a no-op (they're already sent) — the classic beginner bug. WriteHeader(200) then Write(body) is the explicit form; the implicit form (just Write) sends 200.
The concurrency contract: handlers run on their own goroutine per request — shared state between handler calls needs the goroutines topic's synchronization (mutex, channels, or per-request state via context). The handler function runs and returns; the runtime handles the connection lifecycle around it.
Common Mistakes & Pitfalls
Giving a definition without explaining a practical trade-off.
Check your understanding
Answer these without looking back at the article.
- When would you use Http Handlefunc in a real project?
- How would you compare Http Handlefunc with an alternative?
- What is a common mistake when using Http Handlefunc?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.