1.安装httprouter
go get github.com/julienschmidt/httprouter
手册
.代码
main.go
package main
import (
"net/http"
"github.com/julienschmidt/httprouter"
)
func RegisterHandlers() *httprouter.Router {
router := httprouter.New()
router.POST("/user", CreateUser)
router.POST("/user/:user_name", Login)
return router
}
func main() {
registerHandler := RegisterHandlers()
http.ListenAndServe(":8000", registerHandler)
}
Handlers.go
package main
import (
"github.com/julienschmidt/httprouter"
"net/http"
"io"
)
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)
}
网友评论