package main
import (
"fmt"
_ "math/rand"
_ "time"
)
type EndPoint func(req interface{}) interface{}
type Middleware func(EndPoint) EndPoint
func Chain(others ...Middleware) Middleware {
return func(e EndPoint) EndPoint {
for i := len(others) - 1; i >= 0; i-- {
e = others[i](e)
}
return e
}
}
func genMiddleware(i int) Middleware {
return func(next EndPoint) EndPoint {
fmt.Println("flag...", i)
return next
}
}
func StartMain() {
var mids []Middleware
for i := 10; i > 0; i-- {
mids = append(mids, genMiddleware(i))
}
mid := Chain(mids...)
e := func(request interface{}) interface{} {
fmt.Println("start...")
return nil
}
finalfun := mid(e)
finalfun(3)
finalfun(3)
}
时间: 2024-11-02 21:08:11