Go Web:HttpRouter路由(一)
2019独角兽企业重金招聘Python工程师标准>>>
HttpRouter是一个轻量级但却非常高效的multiplexer。
手册:
https://godoc.org/github.com/julienschmidt/httprouter
https://github.com/julienschmidt/httprouter
安装httprouter
go get github.com/julienschmidt/httprouter
用法示例
代码如下:
♠main.go 文件
package mainimport ("github.com/julienschmidt/httprouter""net/http"//"log"
)func RegisterHandlers() *httprouter.Router {//New()方法创建了实例,启动web服务router := httprouter.New()//注册handlerrouter.GET("/", Index)router.GET("/hello/:name", Hello)//注册handlerrouter.POST("/user", CreateUser)router.POST("/user/:user_name", Login)return router
}func main() {registerHandler := RegisterHandlers()//log.Fatal(http.ListenAndServe(":8080", router))http.ListenAndServe(":8080", router)
}
这种模式"/hello/:name",它可以用来做命名匹配,类似于正则表达式的命名捕获分组。
♠Handlers.go 文件
定义httprouter中的handler,处理对应请求
package mainimport ("github.com/julienschmidt/httprouter""net/http""io"
)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 CreateUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {io.WriteString(w, "create User Handler")
}func Login(w http.ResponseWriter, r *http.Request, p httprouter.Params) {userName := p.ByName("user_name")io.WriteString(w, userName)
}
测试
浏览器上输入路由即可
如:
http://localhost:8080/hello/xxxx
浏览器上输出:hello, xxxx!
httprouter用法说明
Variables
func CleanPath(p string) string
type Handle
type Param
type Paramsfunc ParamsFromContext(ctx context.Context) Paramsfunc (ps Params) ByName(name string) string
type Routerfunc New() *Routerfunc (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 HTTPrequests. Like http.HandlerFunc, but has a third parameter for the values ofwildcards (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。
转载于:https://my.oschina.net/u/3683692/blog/3038920
Go Web:HttpRouter路由(一)相关推荐
- Go Web——HttpRouter路由
文章目录 简介 一 快速入门 二 参数 2.1 命名参数 2.2 Catch-All参数 三 HttpRouter 请求方法 四 基于 HttpRouter 的 Web 框架(了解) 简介 HttpR ...
- ASP.NET Web API 路由对象介绍
前言 在ASP.NET.ASP.NET MVC和ASP.NET Web API这些框架中都会发现有路由的身影,它们的原理都差不多,只不过在不同的环境下作了一些微小的修改,这也是根据每个框架的特性来制定 ...
- Go Web:HttpRouter路由
HttpRouter是一个轻量级但却非常高效的multiplexer.手册: https://godoc.org/github.com/julienschmidt/httprouter https:/ ...
- Web API路由和动作选择
前言 本文描述ASP.NET Web API如何把一个HTTP请求路由到控制器的一个特定的Action上.关于路由的总体概述可以参见上一篇教程 http://www.cnblogs.com/aehyo ...
- ASP.NET Web API路由规则(二)
默认的规则 在ASP.NET MVC4中 global.asax.cs代码中并无注册默认路由规则的代码 代码如下: public class WebApiApplication : System.We ...
- Asp.net Web Api 路由 和 异常处理
一.路由: 新建一个ASP.NET MVC4 Web Application项目之后,我们会发现在网站根目录下有个App_Start文件夹.找到下面的RouteConfig.cs文件,如下: publ ...
- Vert.x(vertx) Web开发-路由
在Vert.x 创建HTTP服务 中我们已经创建了一个简单的HttpServer,但这个HttpServer比较低级,对于请求参数解析.Session等常用功能都需要我们通过编码实现,也就是要重复造轮 ...
- Web开发中的路由是什么意思?(关键词:Web开发/路由)
路由就是URL到函数的映射. 在web开发中,"route"是指根据url, 分配到对应的处理程序. 路由: 就是一个路径的解析,根据客户端提交的路径,将请求解析到相应的控制器上: ...
- webapi 路由限制命名控件_解决Web API路由配置支持Area及命名空间参数
usingSystem;usingSystem.Collections.Concurrent;usingSystem.Collections.Generic;usingSystem.Linq;usin ...
最新文章
- 如何启用SAP Cloud Platform的mobile服务
- java护眼色是什么数据,护眼色的RGB值和颜色代码汇总
- HTML5废除元素,HTML5 与 HTML4 的区别(2) - 新增的元素和废除的元素
- html语言中%3c%%%3e中语言,[工学]C语言程序设计习题解答.doc
- springboot服务调用超时_Spring Boot 异步请求和异步调用,一文搞定
- vue - (v-pre、v-cloak、v-once)
- [bzoj1878][SDOI2009]HH的项链
- 使用Mac电脑内置的屏幕共享功能,进行远程桌面管理
- 取出大文件里面的一部分数据
- 《数字信号处理》系统函数的频率响应、零极点和稳定性的实现
- fcm算法matlab实现,fcm算法matlab
- SGD: Maximum margin separating hyperplane程序解析
- Unity Rendering Mode
- 网易云热歌榜评论(爬虫项目)
- [搞笑] 后舍男生最完美的视频收藏全纪录
- Boofuzz协议漏洞挖掘入门教程与使用心得
- 02 Ajax表单提交
- Vue和React的区别到底是什么?
- 气体传感器的市场状况及主要厂商
- 【考研英语语法】状语从句精讲
热门文章
- 交互 点击变色_这个95%的人都没用过的PPT功能,几分钟帮你做出发布会级的交互动画...
- 中标麒麟linux系统忘记root密码,中标麒麟(龙芯CPU)--忘记root密码怎么修改?
- hbuilderx能开发vue吗_学会就能接项目!Vue + Spring Boot 实现全栈商城项目开发
- cnn池化层输入通道数_(pytorch-深度学习系列)CNN中的池化层-学习笔记
- python mac os安装教程_教程:在 Mac OS X 上安装 TensorFlow
- fastdfs redis java,大文件上传_断点续传_文件分片传输_fastdfs_前后端一站式解决方案...
- java编程东西好多记不住_课程总结
- python冷知识_python冷知识
- 异步fifo_FPGA设计基础——FIFO的应用
- 奔跑吧linux内核_别了,Linux 的魔法时代!