参考gin中间件
type Context struct {
handlers []func(c *Context)
index int8
}
func (c *Context) Use(f func(c *Context)) {
c.handlers = append(c.handlers, f)
}
func (c *Context) Next() {
c.index++
c.handlers[c.index](c)
}
func (c *Context) GET(path string, f func(c *Context)) {
c.handlers = append(c.handlers, f)
}
func (c *Context) Run() {
c.handlers[0](c)
}
func main() {
c := &Context{}
c.Use(func(c *Context) {
fmt.Println("mid1 start")
c.Next()
fmt.Println("mid1 end")
})
c.Use(func(c *Context) {
fmt.Println("mid2 start")
c.Next()
fmt.Println("mid2 end")
})
c.GET("?", func(c *Context) {
fmt.Println("GET handler func")
})
c.Run()
}
796

被折叠的 条评论
为什么被折叠?



