gocraft/web 是一个 Go mux 和中间件包,用于实现简单、快速的 Go 语言 Web 框架。
示例代码:
package main import ( "github.com/gocraft/web" "fmt" "net/http" "strings" ) type Context struct { HelloCount int } func (c *Context) SetHelloCount(rw web.ResponseWriter, req *web.Request, next web.NextMiddlewareFunc) { c.HelloCount = 3 next(rw, req) } func (c *Context) SayHello(rw web.ResponseWriter, req *web.Request) { fmt.Fprint(rw, strings.Repeat("Hello ", c.HelloCount), "World!") } func main() { router := web.New(Context{}). // Create your router Middleware(web.LoggerMiddleware). // Use some included middleware Middleware(web.ShowErrorsMiddleware). // ... Middleware((*Context).SetHelloCount). // Your own middleware! Get("/", (*Context).SayHello) // Add a route http.ListenAndServe("localhost:3000", router) // Start the server! }