0PricingLogin
Go Academy · Lesson

HTTP Middleware Chain Fundamentals

Wrapping http.Handler and chaining with next

Middleware in net/http

In Go's standard library, middleware is a function that accepts an http.Handler and returns a new http.Handler with additional behaviour wrapped around the original.

type Middleware func(http.Handler) http.Handler

Basic middleware

The inner HandlerFunc wraps the original handler with pre/post logic:

func Logger(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        log.Printf("→ %s %s", r.Method, r.URL.Path)
        next.ServeHTTP(w, r)
        log.Printf("← %s %s done", r.Method, r.URL.Path)
    })
}

All lessons in this course

  1. HTTP Middleware Chain Fundamentals
  2. Logging and Request ID Middleware
  3. Auth and Rate Limiting Middleware
  4. CORS and Panic Recovery Middleware
← Back to Go Academy