http.HandleFunc 模式中的通配符
注册处理程序时,有没有办法在模式中指定通配符?
例如:
http.HandleFunc("/groups/*/people", peopleInGroupHandler)
其中 *
可以是任何有效的 URL 字符串。或者是匹配 /groups
并从处理程序 (peopleInGroupHandler
) 函数中计算出其余部分的唯一解决方案?
When registering handlers, is there any way to specify wildcards in the pattern?
For example:
http.HandleFunc("/groups/*/people", peopleInGroupHandler)
Where the *
could be any valid URL string. Or is the only solution to match /groups
and figure the rest out from within the handler (peopleInGroupHandler
) func?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
http.Handler 和 http.HandleFunc 的模式不是正则表达式或 glob。没有办法指定通配符。它们记录在此处。
也就是说,创建您自己的可以使用正则表达式或您想要的任何其他类型的模式的处理程序并不太难。这是使用正则表达式的一个(已编译,但未经测试):
The patterns for http.Handler and http.HandleFunc aren't regular expressions or globs. There isn't a way to specify wildcards. They're documented here.
That said, it's not too hard to create your own handler that can use regular expressions or any other kind of pattern you want. Here's one that uses regular expressions (compiled, but not tested):
自 2011 年以来,您现在(2014 年以上)可以找到其他解决方案。
例如,Gorilla Web 工具包的 mux 包提供了所有类型的路由选项:
它可以轻松集成到任何 BYOR(自带路由器)http 库,如 negroni。
以下是文章“Gorilla vs Pat vs Routes:A Mux Showdown”中的示例:
在这种情况下,“魔法”是在“
gorilla/mux/regexp.go
”,以及 在这里测试。这个想法是提取命名变量,组装要匹配的正则表达式,创建“反向”模板来构建 URL 并编译正则表达式以验证 URL 构建中使用的变量值。
Since 2011, you can now (2014+) find other solutions.
For instance, the mux package of the Gorilla Web toolkit provides all kind of routing options:
It can be easily integrated to any BYOR (Bring your own Router) http library, like negroni.
Here is an example from the article "Gorilla vs Pat vs Routes: A Mux Showdown":
In this instance, the "magic" is defined in "
gorilla/mux/regexp.go
", and tested here.The idea is to extract named variables, assemble a regexp to be matched, create a "reverse" template to build URLs and compile regexps to validate variable values used in URL building.
以下是如何使用 @evanshaw 的代码示例的示例
Here's an example of how to use the code example from @evanshaw
我只是想添加
julienschmidt/httprouter
,它的行为类似于net/http
,但带有一个用于 url 值的附加参数和对请求方法的支持:https://github.com/julienschmidt/httprouter
它似乎也比
gorilla/mux
(根据 GitHub),它还声称需要更少的内存。https://github.com/julienschmidt/go-http-routing-benchmark
I just wanted to add
julienschmidt/httprouter
, which just behaves likenet/http
but with an additional parameter for url-values and support for request methods:https://github.com/julienschmidt/httprouter
It also seems to be slightly more popular than
gorilla/mux
(according to GitHub) and it also claims to need less memory.https://github.com/julienschmidt/go-http-routing-benchmark
您可以检查 violetear 如何处理动态+包罗万象(通配符)模式,这只是为了补充,例如:
在本例中,请求可能有 2 个不同的
UUIDS
对于动态/通配符,这可能适用:
可以使用正则表达式来匹配 IP:
或者只是一个捕获所有允许
GET
和仅限HEAD
方法:更多示例可在此处找到:https:// /violetear.org/post/how-it-works/
You could check how violetear handles dynamic + catchall (wildcard) patterns, this is just for complement for example:
In this case, the request may have 2 different
UUIDS
For a dynamic/wildcard this could apply:
A regex may be used to match the IP:
Or simply just a catch all allowing
GET
andHEAD
methods only:More examples can be found here: https://violetear.org/post/how-it-works/
注册处理程序时通常不指定通配符。
包含通配符的路由维护在路由表中,根据用户输入进行编译,然后传递给适当的处理函数。
我偶然发现 Ben Hoyt 关于 Go 中 HTTP 路由的博客,其中他描述了自定义技术并将其与第三方的。我强烈建议任何阅读本文的人都仔细阅读它。另外,Gorilla'x mux 包现已存档
下面的方法基于正则表达式表,其中我们循环遍历预编译的正则表达式并使用请求上下文传递匹配项。
请求和响应示例
Wildcards are usually not specified when registering handlers.
Routes containing wildcards are maintained in a route table, compiled based on user input, and then passed to the appropriate handler function.
I came across Ben Hoyt's blog on HTTP routing in Go in which he describes and compares custom techniques with third-party ones. I would highly recommend anyone reading this to go through it. Also, Gorilla'x mux package is now archived
The below approach is based on regex table, in which we loop through pre-compiled regexes and pass matches using the request context.
Example requests and responses