HttpRouter框架 | 青训营笔记

199 阅读3分钟

这是我参与「第三届青训营 -后端场」笔记创作活动的的第11篇笔记

😆在这里,我对今天所新学到的Golang的HTTP Router框架做了一次总结

😜Golang的其他知识在哪里找呢,那你就问对了

👨‍💻Golang基础复习 - 掘金 (juejin.cn) 在这里我总结了一些这篇文章没有提到的一些知识

😊如果有小伙伴能想到更多知识,欢迎大家在评论区留言,那么我们就开始吧

👩‍💻👨‍💻哟西,一个棕~

😎😎😎我是小小分割线

介绍

HttpRouter是一种轻量级高性能的请求路由器

号称比原生的路由要快50倍

有很多框架就是基于它为基础开发的

就比如我们的gin,在这里贴出gin的官网:Gin Web Framework (gin-gonic.com)

下载安装

我们可以在Golang官方文档搜索:pkg.go.dev

找到仓库地址:httprouter package - github.com/julienschmidt/httprouter - pkg.go.dev

image.png

使用Go get 进行下载

也就是使用以下命名进行下载

go get github.com/julienschmidt/httprouter

使用和分析

此时我们可以使用官方给的案例来试一试

// 可以有第三个参数Params,这个函数忽略了它,在下一个函数使用了它
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
   fmt.Fprint(w, "Welcome!\n")
}

// 这个函数使用了它
// 在下面会介绍它的样子和ByName()的作用
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
   fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}

func main() {
   // new一个路由
   router := httprouter.New()

   // 开启两个路由
   router.GET("/", Index)
   router.GET("/hello/:name", Hello)

   log.Fatal(http.ListenAndServe(":8080", router))
}

我们来看一下第三个参数Params是什么样子的

// Params is a Param-slice, as returned by the router.
// The slice is ordered, the first URL parameter is also the first slice value.
// It is therefore safe to read values by the index.
type Params []Param

// ByName returns the value of the first Param which key matches the given name.
// If no matching Param is found, an empty string is returned.
func (ps Params) ByName(name string) string {
   for i := range ps {
      if ps[i].Key == name {
         return ps[i].Value
      }
   }
   return ""
}

可以看到:

这个参数Params 是一个 Param-slice

由路由器返回

切片是有序的,第一个 URL 参数也是第一个切片值,通过索引读取值是安全的。

也就是说使用来返回路由中的值的

我们来看它的ByName():

ByName 返回与给定名称匹配的第一个参数的值。

如果没有找到匹配的 Param,则返回一个空字符串。

那我们知道了Hello这个handler

使用ByName("name")来对路由中/:name取值

并传递到writer上打印

我们来看看效果是不是如我们所料

image.png

果然如我们所料

除了这一点,再看看我们的开启服务器时可以调用的api

// GET is a shortcut for router.Handle(http.MethodGet, path, handle)
func (r *Router) GET(path string, handle Handle) {
   r.Handle(http.MethodGet, path, handle)
}

// HEAD is a shortcut for router.Handle(http.MethodHead, path, handle)
func (r *Router) HEAD(path string, handle Handle) {
   r.Handle(http.MethodHead, path, handle)
}

// OPTIONS is a shortcut for router.Handle(http.MethodOptions, path, handle)
func (r *Router) OPTIONS(path string, handle Handle) {
   r.Handle(http.MethodOptions, path, handle)
}

// POST is a shortcut for router.Handle(http.MethodPost, path, handle)
func (r *Router) POST(path string, handle Handle) {
   r.Handle(http.MethodPost, path, handle)
}

// PUT is a shortcut for router.Handle(http.MethodPut, path, handle)
func (r *Router) PUT(path string, handle Handle) {
   r.Handle(http.MethodPut, path, handle)
}

// PATCH is a shortcut for router.Handle(http.MethodPatch, path, handle)
func (r *Router) PATCH(path string, handle Handle) {
   r.Handle(http.MethodPatch, path, handle)
}

// DELETE is a shortcut for router.Handle(http.MethodDelete, path, handle)
func (r *Router) DELETE(path string, handle Handle) {
   r.Handle(http.MethodDelete, path, handle)
}

不仅可以开启GET

还可以POST、PUT、DELETE、PATCH

那我们一看,这不就是我们需要的Restful风格吗?

没错,通过它可以达到Restful风格


这就是HttpRouter这个框架的简单介绍

😎😎😎又是我,我还是小小分割线

都用心看到这里了,那就求个赞吧😘

🥳🥳🥳如果小伙伴有其他的小知识,一定不要忘了在评论区讨论哟,多多讨论,生态才会越来越好