๐—•๐˜‚๐—ถ๐—น๐—ฑ๐—ถ๐—ป๐—ด ๐—š๐—ผ ๐— ๐—ถ๐—ฑ๐—ฑ๐—น๐—ฒ๐˜„๐—ฎ๐—ฟ๐—ฒ ๐—™๐—ฟ๐—ผ๐—บ ๐—ฆ๐—ฐ๐—ฟ๐—ฎ๐˜๐—ฐ๐—ต

Every Go web framework uses middleware. Gin and Echo have it. Most tutorials teach you how to use it. Few teach you how it works.

Go HTTP logic relies on one interface: http.Handler.

A middleware is a function. It takes a handler and returns a handler. It wraps the next step in the process. It runs code before or after the main handler.

Manual wrapping is messy. You end up with nested functions. A Chain function fixes this. It takes a slice of middleware. It applies them in the right order.

You build production tools with this pattern:

This pattern is not magic. It is a series of nested function calls.

You do not always need a framework. The standard library handles most API needs.

Source: https://dev.to/shayan_holakouee/building-a-nethttp-middleware-chain-from-scratch-in-go-346b