t/http package already includes a utility for this (called MaxBytesReader) which makes writing the middleware quite simple.
Note that the way I write middleware is heavily inspired by Mat Ryer&rsquo;s adapter pattern.
Here&rsquo;s the code:
// Represents a piece of HTTP middleware.
type Middleware func(http.Handler) http.Handler

// Limit the size of the request body to 4KB.
const MaxRequestBodySize = 4 * 1024

// Places an upper limit on the size of every request body.
func LimitRequestBodySize() Middlewar