Skip to content

Instantly share code, notes, and snippets.

@iMega
Created August 30, 2018 15:19
Show Gist options
  • Save iMega/407a3a8ae7241e11aadcf87ffd501150 to your computer and use it in GitHub Desktop.
Save iMega/407a3a8ae7241e11aadcf87ffd501150 to your computer and use it in GitHub Desktop.
best practice append
// A constructor for a piece of middleware.
// Some middleware use this constructor out of the box,
// so in most cases you can just pass somepackage.New
type Constructor func(http.Handler) http.Handler
// Chain acts as a list of http.Handler constructors.
// Chain is effectively immutable:
// once created, it will always hold
// the same set of constructors in the same order.
type Chain struct {
constructors []Constructor
}
// New creates a new chain,
// memorizing the given list of middleware constructors.
// New serves no other function,
// constructors are only called upon a call to Then().
func New(constructors ...Constructor) Chain {
return Chain{append(([]Constructor)(nil), constructors...)}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment