Go Web:HttpRouter路由(一)
2019獨角獸企業重金招聘Python工程師標準>>>
HttpRouter是一個輕量級但卻非常高效的multiplexer。
手冊:
https://godoc.org/github.com/julienschmidt/httprouter https://github.com/julienschmidt/httprouter安裝httprouter
go get github.com/julienschmidt/httprouter
用法示例
代碼如下:
?main.go 文件
package mainimport ("github.com/julienschmidt/httprouter""net/http"//"log" )func RegisterHandlers() *httprouter.Router {//New()方法創建了實例,啟動web服務router := httprouter.New()//注冊handlerrouter.GET("/", Index)router.GET("/hello/:name", Hello)//注冊handlerrouter.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 mainimport ("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) }測試
瀏覽器上輸入路由即可
如:
http://localhost:8080/hello/xxxx瀏覽器上輸出:hello, xxxx!
httprouter用法說明
Variables func CleanPath(p string) string type Handle type Param type Paramsfunc ParamsFromContext(ctx context.Context) Paramsfunc (ps Params) ByName(name string) string type Routerfunc New() *Routerfunc (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 Handlehttprouter中的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 HTTPrequests. Like http.HandlerFunc, but has a third parameter for the values ofwildcards (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。
轉載于:https://my.oschina.net/u/3683692/blog/3038920
總結
以上是生活随笔為你收集整理的Go Web:HttpRouter路由(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最新设备可利用积雪发电?UCLA研究出积
- 下一篇: 设置表字段大小写敏感