05 / 06

How would you implement middleware in Go using the standard library pattern vs a framework?

Middleware in Go is a function that wraps an http.Handler and returns a new http.Handler. This pattern is the same whether using the standard library or frameworks like Gin and Echo.

Middleware pattern
Framework comparison
  1. 1

    Standard library: no dependencies, explicit, full control — preferred for simple services

  2. 2

    Gin: Use() method chains middleware; request context via gin.Context; fast router with params

  3. 3

    Echo: similar to Gin, cleaner API, built-in binder and validator

  4. 4

    Chi: 100% compatible with net/http; lightweight router with middleware support

  5. 5

    For most APIs: net/http + Chi or stdlib routing (Go 1.22) is sufficient without a full framework