gorilla/mux请求路由和分发的 Go 框架
gorilla/mux
实现了一个请求路由和分发的 Go 框架。
mux 名字的意思是 "HTTP request multiplexer". 和标准包 http.ServeMux
类似, mux.Router
根据已注册路由列表匹配传入请求,并调用与URL或其他条件匹配的路由的处理程序。
主要特性:
- It implements the
http.Handler
interface so it is compatible with the standardhttp.ServeMux
. - Requests can be matched based on URL host, path, path prefix, schemes, header and query values, HTTP methods or using custom matchers.
- URL hosts, paths and query values can have variables with an optional regular expression.
- Registered URLs can be built, or "reversed", which helps maintaining references to resources.
- Routes can be used as subrouters: nested routes are only tested if the parent route matches. This is useful to define groups of routes that share common conditions like a host, a path prefix or other repeated attributes. As a bonus, this optimizes request matching.
安装
go get -u github.com/gorilla/mux
代码示例
func main() { r := mux.NewRouter() r.HandleFunc("/", HomeHandler) r.HandleFunc("/products", ProductsHandler) r.HandleFunc("/articles", ArticlesHandler) http.Handle("/", r) }
这里我们注册了三个 URL 匹配路由进行处理。路径也可以是变量:
r := mux.NewRouter() r.HandleFunc("/products/{key}", ProductHandler) r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler) r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
这些名称用于创建路由变量的映射,可以通过调用mux.Vars 获取:
func ArticlesCategoryHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "Category: %v\n", vars["category"]) }
评论
HttpRouterHTTP 请求路由
HttpRouter是一个Go语言开发的轻量级高性能HTTP请求路由,也可以叫多路复用。示例代码:package mainimport ( "fmt" "github.com/julien
HttpRouterHTTP 请求路由
0
mux功能完备的 Go 路由器
mux是对http.ServeMux的扩展,添加正则路由等功能。-路由参数;-支持正则表达式作为路由项匹配方式;-拦截正则表达式的行为;-自动生成OPTIONS请求处理方式;-自动生成HEAD请求处理
mux功能完备的 Go 路由器
0
ICERESTRESTful 路由框架
ICEREST是一个非常轻量级只有200k左右的RESTful路由框架,通过ICEREST你可以处理url的解析,数据的封装,Json的输出,和传统的方法融合,请求的参数便是方法的参数,方法的返回值便
ICERESTRESTful 路由框架
0