其他分享
首页 > 其他分享> > 第三方路由库httprouter的基本操作

第三方路由库httprouter的基本操作

作者:互联网

package main

import (
    "net/http"
    "github.com/julienschmidt/httprouter"
    "io"
)

//第三个参数p是httprouter新增的
func CreateUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
    //io.WriteString(w, "create user")
    io.WriteString(w, p.ByName("user_name") + "\n")
    //如果没有的则不会输出
    io.WriteString(w, p.ByName("fffffff") + "\n")
    io.WriteString(w, p.ByName(("pwd")) + "\n")
}

//使用httprouter
func RegisterHandlers() *httprouter.Router {
    router := httprouter.New()
    router.GET("/user/:user_name/pwd/:pwd", CreateUser)
    return router
}

func main() {
    r := RegisterHandlers()
    http.ListenAndServe(":8000", r)
}

输出:
第三方路由库httprouter的基本操作

标签:http,WriteString,pwd,httprouter,io,基本操作,路由,user
来源: https://blog.51cto.com/5660061/2366868