HttpRouter是一个轻量级但却很是高效的multiplexer。git
手册:github
https://godoc.org/github.com/julienschmidt/httprouter https://github.com/julienschmidt/httprouter
go get github.com/julienschmidt/httprouter
代码以下:web
♠main.go 文件
正则表达式
package main import ( "github.com/julienschmidt/httprouter" "net/http" //"log" ) func RegisterHandlers() *httprouter.Router { //New()方法建立了实例,启动web服务 router := httprouter.New() //注册handler router.GET("/", Index) router.GET("/hello/:name", Hello) //注册handler router.POST("/user", CreateUser) router.POST("/user/:user_name", Login) return router } func main() { registerHandler := RegisterHandlers() //log.Fatal(http.ListenAndServe(":8080", router)) http.ListenAndServe(":8080", router) }
这种模式"/hello/:name",它能够用来作命名匹配,相似于正则表达式的命名捕获分组。浏览器
♠Handlers.go 文件
函数
定义httprouter中的handler,处理对应请求测试
package main import ( "github.com/julienschmidt/httprouter" "net/http" "io" ) func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprint(w, "Welcome!\n") } func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name")) } func CreateUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) { io.WriteString(w, "create User Handler") } func Login(w http.ResponseWriter, r *http.Request, p httprouter.Params) { userName := p.ByName("user_name") io.WriteString(w, userName) }
浏览器上输入路由便可code
如:router
http://localhost:8080/hello/xxxx
浏览器上输出:hello, xxxx!接口
Variables func CleanPath(p string) string type Handle type Param type Params func ParamsFromContext(ctx context.Context) Params func (ps Params) ByName(name string) string type Router func New() *Router func (r *Router) DELETE(path string, handle Handle) func (r *Router) GET(path string, handle Handle) func (r *Router) HEAD(path string, handle Handle) func (r *Router) Handle(method, path string, handle Handle) func (r *Router) Handler(method, path string, handler http.Handler) func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc) func (r *Router) Lookup(method, path string) (Handle, Params, bool) func (r *Router) OPTIONS(path string, handle Handle) func (r *Router) PATCH(path string, handle Handle) func (r *Router) POST(path string, handle Handle) func (r *Router) PUT(path string, handle Handle) func (r *Router) ServeFiles(path string, root http.FileSystem) func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) type Handle
httprouter中的Handle相似于http.HandlerFunc,只不过它支持第三个参数Params。
type Handle func(http.ResponseWriter, *http.Request, Params) Handle is a function that can be registered to a route to handle HTTP requests. Like http.HandlerFunc, but has a third parameter for the values of wildcards (variables).
例如前面示例中的Index()和Hello()都是Handle类型的实例。
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprint(w, "Welcome!\n") } func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name")) }
注册handler httprouter.Router类型相似于http包中的ServeMux,它实现了http.Handler接口,因此它是一个http.Handler。它能够将请求分配给注册好的handler。
type Router struct {} func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
除此以外,Router提供了很多方法,用来指示如何为路径注册handler。
func (r *Router) Handle(method, path string, handle Handle) func (r *Router) Handler(method, path string, handler http.Handler) func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc)
httprouter.Handle()用于为路径注册指定的Handle,而httprouter.Handle对应于http.HandlerFunc,因此是直接将Handle类型的函数绑定到指定路径上。同时,它还能够指定http方法:GET, POST, HEAD, PUT, PATCH, DELETE, OPTIONS。