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路由使用方法详解请查看下面的相关链接

(0)

相关推荐

  • golang常用库之gorilla/mux-http路由库使用详解

    golang常用库:gorilla/mux-http路由库使用 golang常用库:配置文件解析库-viper使用 golang常用库:操作数据库的orm框架-gorm基本使用 一:golang自带路由介绍 golang自带路由库 http.ServerMux ,实际上是一个 map[string]Handler,是请求的url路径和该url路径对于的一个处理函数的映射关系.这个实现比较简单,有一些缺点: 不支持参数设定,例如/user/:uid 这种泛型类型匹配无法很友好的支持REST模式,无

  • Golang中的路由使用详解

    之前有篇文章比较浅显的分析了一下golang的服务器如何实现,还有Handler, DefaultServeMux,HandlerFunc的用处. 我们现在已经明白了DefaultServeMux就是存放pattern和handler的地方,我们称其为路由,那么我们可能会想,既然golang能够实现这个路由,我们能否也模仿一个呢? 首先我们需要一个能够保存客户端的请求的一个容器(路由). 创建路由结构体 type CopyRouter struct { router map[string]map

  • golang微服务框架基础Gin基本路由使用详解

    目录 概述 1. 基本路由 2. 路由参数 获取URL路径全部参数 获取URL路径单个参数 获取URL中指定的参数 获取指定默认值的参数的 概述 路由是自定义url地址执行指定的函数,良好的路由定义可以对seo起到很好的效果. 1. 基本路由 gin框架封装了http库,提供了 GET.POST.PUT.DELETE.PATCH.HEAD.OPTIONS 这些http请求方式. 使用 router.method() 来绑定路由 func (group *RouterGroup) METHOD(r

  • golang beego框架路由ORM增删改查完整案例

    目录 程序运行前加载 路由设置 高级路由设置 beego-ORM初始化 安装ORM+导包 定义结构体 beego支持的数据库 连接数据库 注册数据库表 生成表 完整案例 ORM增删改查 插入 查询 更新 删除 案例 注册 案例2 程序运行前加载 1.导包前面加下划线,运行前加载 2.把要加载的写在init函数里面 路由设置 路由的作用:根据不同的请求指定不同的控制器 路由函数: beego.Router("/path",&controller.MainController{})

  • 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" "

  • C语言memset函数使用方法详解

    C语言memset函数使用方法详解 一.函数原形   void *  memset(void*s, int ch,size_t n) 二.函数作用  将以s内存地址为首的连续n个字节的内容置成ch,一般用来对大量结构体和数组进行清零 三.常见错误 1.搞反了 ch 和 n的位置 对char[20]清零,一定是 memset(a,0,20); 2.过度使用memset 3.其实这个错误严格来讲不能算用错memset,但是它经常在使用memset的场合出现 int fun(strucy someth

  • C语言system函数使用方法详解

    目录 函数接口 作用 返回值 测试代码 参数 MODE命令 color命令 函数接口 _DCRTIMP int __cdecl system( _In_opt_z_ char const* _Command ); system函数已经被收录在标准c库中,头文件为<stdlib.h> 作用 执行系统命令调用命令处理器来执行命令. 如果命令是空指针,则该函数只检查是否有命令处理器可以被此函数使用. 命令非空则调用命令的效果取决于系统和库的实现,并可能导致程序以非标准的方式运行或终止. 补充: 执行

  • C语言 操作符#与##使用方法详解

    目录 一.# 运算符 二.## 运算符 三.小结 一.# 运算符 # 运算符用于在预处理期将宏参数转换为字符串 # 的转换作用是在预处理期完成的,因此只在宏定义中有效 编译器不知道 # 的转换作用 用法: #define STRING(x) #x printf("%s\n",STRING(Hello World!)); 下面通过一个示例感受一下: test.c: #include <stdio.h> #define STRING(x) #x int main() { pri

  • C语言实现面向对象的方法详解

    目录 1.引言 2.封装 3.继承 4.多态 4.1 虚表和虚指针 4.2 在构造函数中设置vptr 4.3 继承 vtbl 和 重载 vptr 4.4 虚函数调用 4.5 main.c 5.总结 1.引言 面向对象编程(OOP)并不是一种特定的语言或者工具,它只是一种设计方法.设计思想.它表现出来的三个最基本的特性就是封装.继承与多态.很多面向对象的编程语言已经包含这三个特性了,例如 Smalltalk.C++.Java.但是你也可以用几乎所有的编程语言来实现面向对象编程,例如 ANSI-C.

  • C语言qsort函数使用方法详解

    目录 1.qsort函数 1.1qsort函数功能 1.2参数介绍 2.qsort函数功能测试 3.冒泡排序思想模拟实现qsort 1.qsort函数 void qsort (void* base, size_t num, size_t size, int (compar)(const void,const void*)); 1.1qsort函数功能 可以排序任何数据类型 对 所指向的数组元素进行排,使用函数确定顺序. 此函数使用的排序算法通过调用指定函数并指向元素的指针作为参数来比较元素. 该

  • Spring根据URL参数进行路由的方法详解

    前言 本文主要介绍了关于Spring根据URL参数进行路由的相关内容,分享出来供大家参考学习价值,下面来一起看看详细的介绍吧. 发现问题 最近在写接口的时候发现一个问题,就是两个REST接口的URL的path部分是一样的,根据query传入不同的参数来区分. 比如S3普通上传接口是是: PUT /{bucketname}/{ objectname} 分块上传的接口是: PUT /{bucketname}/{objectname}?partNumber={partNumber}&uploadId=

  • C语言单链表实现方法详解

    本文实例讲述了C语言单链表实现方法.分享给大家供大家参考,具体如下: slist.h #ifndef __SLIST_H__ #define __SLIST_H__ #include<cstdio> #include<malloc.h> #include<assert.h> typedef int ElemType; typedef struct Node { //定义单链表中的结点信息 ElemType data; //结点的数据域 struct Node *next

  • Laravel框架创建路由的方法详解

    本文实例讲述了Laravel框架创建路由的方法.分享给大家供大家参考,具体如下: 我这里使用的Laravel版本是5.6,路由位置在routes/web.php中,所以我们在这个文件中添加我们想要添加的路由. 1.基础路由 //get请求,结果如下图 Route::get('basic1',function (){ return 'Hello World'; }); //post请求,这里不展示结果图 Route::post('basic2',function (){ return 'Post'

  • vue 开发之路由配置方法详解

    本文实例讲述了vue 开发之路由配置方法.分享给大家供大家参考,具体如下: 概要 用 Vue.js + vue-router 创建单页应用,是非常简单的.使用 Vue.js ,我们已经可以通过组合组件来组成应用程序,当你要把 vue-router 添加进来,我们需要做的是,将组件(components)映射到路由(routes),然后告诉 vue-router 在哪里渲染它们. 实现代码 1.在main.js 中引入 router.3 import router from './router/i

随机推荐