Skip to content

Instantly share code, notes, and snippets.

@daydaygo
Created January 15, 2021 04:06
Show Gist options
  • Save daydaygo/c54968eb534bd3a5ad45221905057462 to your computer and use it in GitHub Desktop.
Save daydaygo/c54968eb534bd3a5ad45221905057462 to your computer and use it in GitHub Desktop.
http middleware
// http middleware 实现
type Context struct {
handlers []HandlerFunc
index int
}
func (c *Context) Next() {
c.index++
s := len(c.handlers)
for ; c.index < s; c.index++ {
c.handlers[c.index](c)
}
// HandlerFunc
func A(c *Context) {
part1
c.Next()
part2
}
func B(c *Context) {
part3
c.Next()
part4
}
// chain 洋葱模型: part1 -> part3 -> part4 -> part2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment