Created
January 15, 2021 04:06
-
-
Save daydaygo/c54968eb534bd3a5ad45221905057462 to your computer and use it in GitHub Desktop.
http middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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