Go Web Frameworks interview question · Most asked
What is the http.Handler interface, and why is it central to Go web development?
Summary
http.Handler has one method, ServeHTTP(http.ResponseWriter, *http.Request). Routers, middleware, frameworks and servers all speak this interface, so pieces compose freely.
Longer Answer
http.HandlerFunc adapts a plain function to the interface. Middleware are functions func(http.Handler) http.Handler. Because Chi, Gin and Echo engines are themselves http.Handlers, they run under http.Server, test with httptest, and can host standard middleware.
Code Example
func logging(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.URL.Path)
next.ServeHTTP(w, r)
})
}
Key Points
- One method: ServeHTTP.
- HandlerFunc adapts functions.
- Middleware wrap handlers.
- Frameworks are Handlers too (except Fiber).
Deep dive: Go Web Frameworks: Start with net/http
Explanation
What interviewers look for
Understanding the interface and how middleware composes around it.
More most-asked Go Web Frameworks questions
- How do you test HTTP handlers in Go?
- Compare Gin, Echo, Chi and Fiber.
- How should a Go service use database/sql correctly?
- How do you implement graceful shutdown for a Go HTTP server?
- How do you design consistent error responses in a Go API?
- How do you pass request-scoped values (like the current user) through middleware in Go?
- Do you need a web framework in Go? What does Go 1.22's ServeMux provide?