文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
URL Routing
For some simple applications, the default http.ServeMux
can take you pretty far. If you need more power in how you parse URL endpoints and route them to the proper handler, you may need to pull in a third party routing framework. For this tutorial, we will use the popular github.com/julienschmidt/httprouter
library as our router. github.com/julienschmidt/httprouter
is a great choice for a router as it is a very simple implementation with one of the best performance benchmarks out of all the third party Go routers.
In this example, we will create some routing for a RESTful resource called "posts". Below we define mechanisms to view index, show, create, update, destroy, and edit posts.
package main
import (
"fmt"
"net/http"
"github.com/julienschmidt/httprouter"
)
func main() {
r := httprouter.New()
r.GET("/", HomeHandler)
// Posts collection
r.GET("/posts", PostsIndexHandler)
r.POST("/posts", PostsCreateHandler)
// Posts singular
r.GET("/posts/:id", PostShowHandler)
r.PUT("/posts/:id", PostUpdateHandler)
r.GET("/posts/:id/edit", PostEditHandler)
fmt.Println("Starting server on :8080")
http.ListenAndServe(":8080", r)
}
func HomeHandler(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
fmt.Fprintln(rw, "Home")
}
func PostsIndexHandler(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
fmt.Fprintln(rw, "posts index")
}
func PostsCreateHandler(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
fmt.Fprintln(rw, "posts create")
}
func PostShowHandler(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
id := p.ByName("id")
fmt.Fprintln(rw, "showing post", id)
}
func PostUpdateHandler(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
fmt.Fprintln(rw, "post update")
}
func PostDeleteHandler(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
fmt.Fprintln(rw, "post delete")
}
func PostEditHandler(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
fmt.Fprintln(rw, "post edit")
}
Exercises
- Explore the documentation for
github.com/julienschmidt/httprouter
. - Find out how well
github.com/julienschmidt/httprouter
plays nicely with existinghttp.Handler
s likehttp.FileServer
httprouter
has a very simple interface. Explore what kind of abstractions can be built on top of this fast router to make building things like RESTful routing easier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论