Go Web:HttpRouter路由(一)

HttpRouter是一个轻量级但却非常高效的multiplexer。

手册:

https://godoc.org/github.com/julienschmidt/httprouter
https://github.com/julienschmidt/httprouter

安装httprouter


go get github.com/julienschmidt/httprouter

用法示例


代码如下:

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)
}

测试

浏览器上输入路由即可

如:

http://localhost:8080/hello/xxxx

浏览器上输出:hello, xxxx!

httprouter用法说明


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。

转载于:https://my.oschina.net/u/3683692/blog/3038920

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值