Go语言HttpRouter路由使用方法详解

 更新时间:2022年04月18日 10:52:04   作者:骏马金龙  
这篇文章主要介绍了Go语言HttpRouter路由使用方法详解,需要的朋友可以参考下

HttpRouter是一个轻量级但却非常高效的multiplexer。手册:

https://godoc.org/github.com/julienschmidt/httprouter

https://github.com/julienschmidt/httprouter

用法示例

package main

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

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}

func main() {
    router := httprouter.New()
    router.GET("/", Index)
    router.GET("/hello/:name", Hello)

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

首先执行:

go get github.com/julienschmidt/httprouter

然后再启动web服务:

go run xxx.go

和http包的ServeMux用法其实很类似。上面定义了两个httprouter中的handle,类似于http包中的http.HandlerFunc类型,具体的对应关系后文会解释,只要认为它是handler,是处理对应请求的就行了。然后使用New()方法创建了实例,并使用GET()方法为两个模式注册了对应的handler。

需要注意的是,第二个模式"/hello/:name",它可以用来做命名匹配,类似于正则表达式的命名捕获分组。后面会详细解释用法。

httprouter用法说明

Variables
func CleanPath(p string) string
type Handle
type Param
type Params
	func ParamsFromContext(ctx context.Context) Params
	func (ps Params) ByName(name string) string
type Router
	func New() *Router
	func (r *Router) DELETE(path string, handle Handle)
	func (r *Router) GET(path string, handle Handle)
	func (r *Router) HEAD(path string, handle Handle)
	func (r *Router) Handle(method, path string, handle Handle)
	func (r *Router) Handler(method, path string, handler http.Handler)
	func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc)
	func (r *Router) Lookup(method, path string) (Handle, Params, bool)
	func (r *Router) OPTIONS(path string, handle Handle)
	func (r *Router) PATCH(path string, handle Handle)
	func (r *Router) POST(path string, handle Handle)
	func (r *Router) PUT(path string, handle Handle)
	func (r *Router) ServeFiles(path string, root http.FileSystem)
	func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

type Handle

httprouter中的Handle类似于http.HandlerFunc,只不过它支持第三个参数Params。

type Handle func(http.ResponseWriter, *http.Request, Params)
    Handle is a function that can be registered to a route to handle HTTP
    requests. Like http.HandlerFunc, but has a third parameter for the values of
    wildcards (variables).

例如前面示例中的Index()和Hello()都是Handle类型的实例。

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}

注册handler

httprouter.Router类型类似于http包中的ServeMux,它实现了http.Handler接口,所以它是一个http.Handler。它可以将请求分配给注册好的handler。

type Router struct {}
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

除此之外,Router提供了不少方法,用来指示如何为路径注册handler。

func (r *Router) Handle(method, path string, handle Handle)
func (r *Router) Handler(method, path string, handler http.Handler)
func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc)

httprouter.Handle()用于为路径注册指定的Handle,而httprouter.Handle对应于http.HandlerFunc,所以是直接将Handle类型的函数绑定到指定路径上。同时,它还可以指定http方法:GET, POST, HEAD, PUT, PATCH, DELETE, OPTIONS。

这些方法还有对应的各自缩写:

func (r *Router) DELETE(path string, handle Handle)
func (r *Router) GET(path string, handle Handle)
func (r *Router) HEAD(path string, handle Handle)
func (r *Router) OPTIONS(path string, handle Handle)
func (r *Router) PATCH(path string, handle Handle)
func (r *Router) POST(path string, handle Handle)
func (r *Router) PUT(path string, handle Handle)

例如,Get()等价于route.Handle("GET", path, handle)。

例如上面的示例中,为两个路径注册了各自的httprouter.Handle函数。

router := httprouter.New()
router.GET("/", Index)
router.GET("/hello/:name", Hello)

Handler()方法是直接为指定http方法和路径注册http.Handler;HandlerFunc()方法则是直接为指定http方法和路径注册http.HandlerFunc。

Param相关

type Param struct {
    Key   string
    Value string
}
Param is a single URL parameter, consisting of a key and a value.

type Params []Param
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.

func (ps Params) ByName(name string) string
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.

Param类型是key/value型的结构,每个分组捕获到的值都会保存为此类型。正如前面的示例中:

router.GET("/hello/:name", Hello)

这里的:name就是key,当请求的URL路径为/hello/abc,则key对应的value为abc。也就是说保存了一个Param实例:

Param{
	Key: "name",
	Value: "abc",
}

更多的匹配用法稍后解释。

Params是Param的slice。也就是说,每个分组捕获到的key/value都存放在这个slice中。

ByName(str)方法可以根据Param的Key检索已经保存在slice中的Param的Value。正如示例中:

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}

router.GET("/hello/:name", Hello)

这里ByName("name")将检索保存在slice中,Key="name"的Param,且返回这个Param中的Value。

由于Params是slice结构,除了ByName()方法可以检索key/value,通过slice的方法也可以直接检索:

ps[0].Key
ps[0].Value

路径匹配规则

httprouter要为路径注册handler的适合,路径可以进行命名捕获。有两种命名捕获的方式:

Syntax    Type
:name     named parameter
*name     catch-all parameter

其中:name的捕获方式是匹配内容直到下一个斜线或者路径的结尾。例如要为如下路径注册handler:

Path: /blog/:category/:post

当请求路径为:

/blog/go/request-routers            match: category="go", post="request-routers"
/blog/go/request-routers/           no match, but the router would redirect
/blog/go/                           no match
/blog/go/request-routers/comments   no match

*name的捕获方式是从指定位置开始(包含前缀"/")匹配到结尾:

Path: /files/*filepath

/files/                             match: filepath="/"
/files/LICENSE                      match: filepath="/LICENSE"
/files/templates/article.html       match: filepath="/templates/article.html"
/files                              no match, but the router would redirect

再解释下什么时候会进行重定向。在Router类型中,第一个字段控制尾随斜线的重定向操作:

type Router struct {
    RedirectTrailingSlash bool
	...
}

如果请求的URL路径包含或者不包含尾随斜线时,但在注册的路径上包含了或没有包含"/"的目标上定义了handler,但是会进行301重定向。简单地说,不管URL是否带尾随斜线,只要注册路径不存在,但在去掉尾随斜线或加上尾随斜线的路径上定义了handler,就会自动重定向。

例如注册路径为/foo,请求路径为/foo/,会重定向。

下面还有几种会重定向的情况:

注册路径:/blog/:category/:post
请求URL路径:/blog/go/request-routers/

注册路径:/blog/:category
请求URL路径:/blog/go

注册路径:/files/*filepath
请求URL路径:/files

Lookup()

func (r *Router) Lookup(method, path string) (Handle, Params, bool)

Lookup根据method+path检索对应的Handle,以及Params,并可以通过第三个返回值判断是否会进行重定向。

更多关于Go语言HttpRouter路由使用方法详解请查看下面的相关链接

相关文章

  • golang中日期操作之日期格式化及日期转换

    golang中日期操作之日期格式化及日期转换

    在编程中,程序员会经常使用到日期相关操作,下面这篇文章主要给大家介绍了关于golang中日期操作之日期格式化及日期转换的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • go内存缓存BigCache之Entry封装源码阅读

    go内存缓存BigCache之Entry封装源码阅读

    这篇文章主要介绍了go内存缓存BigCache之Entry封装源码阅读
    2023-09-09
  • golang使用iconv报undefined:XXX的问题处理方案

    golang使用iconv报undefined:XXX的问题处理方案

    这篇文章主要介绍了golang使用iconv报undefined:XXX的问题处理方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • 详解Go语言的context包从放弃到入门

    详解Go语言的context包从放弃到入门

    这篇文章主要介绍了Go语言的context包从放弃到入门,本文通过实例演示给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • 使用Go语言进行条件编译的示例代码

    使用Go语言进行条件编译的示例代码

    Go的条件编译主要通过构建标签(build tags)和构建约束(build constraints)来实现,这些标签和约束可以让我们针对不同的操作系统、架构或特定条件编写特定代码,本文给大家介绍了如何使用Go语言进行条件编译,需要的朋友可以参考下
    2024-06-06
  • golang 项目打包部署环境变量设置方法

    golang 项目打包部署环境变量设置方法

    最近将 golang 项目打包部署在不同环境,下面分享一下我的心得体会,对golang 项目打包部署环境变量设置方法感兴趣的朋友一起看看吧
    2024-07-07
  • GoLang OS包以及File类型详细讲解

    GoLang OS包以及File类型详细讲解

    go中对文件和目录的操作主要集中在os包中,下面对go中用到的对文件和目录的操作,做一个总结笔记。在go中的文件和目录涉及到两种类型,一个是type File struct,另一个是type Fileinfo interface
    2023-03-03
  • 代码之美:探索Go语言断行规则的奥秘

    代码之美:探索Go语言断行规则的奥秘

    Go语言是一门以简洁、清晰和高效著称的编程语言,而断行规则是其代码风格的重要组成部分,通过深入研究Go语言的断行规则,我们可以更好地理解和编写优雅的代码,本文将从语法规范、代码风格和最佳实践等方面进行探讨,帮助读者更好地理解和应用Go语言的断行规则
    2023-10-10
  • 一文详解如何使用 Golang 处理文件

    一文详解如何使用 Golang 处理文件

    Golang 是一种强类型、静态编译、并发性高的编程语言,我们将重点介绍 Golang 中的文件基本操作,包括创建文件与查看状态,重命名与移动,删除与截断,读写文件,以及权限控制,跟着小编一起来学习吧
    2023-04-04
  • go语言中基本数据类型及应用快速了解

    go语言中基本数据类型及应用快速了解

    这篇文章主要为大家介绍了go语言中基本数据类型应用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07

最新评论