返回介绍

URL Routing

发布于 2025-02-25 22:26:26 字数 2618 浏览 0 评论 0 收藏 0

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

  1. Explore the documentation for github.com/julienschmidt/httprouter .
  2. Find out how well github.com/julienschmidt/httprouter plays nicely with existing http.Handler s like http.FileServer
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文