如何解析格式为 /id/123 而不是 ?foo=bar 的 URL?

huangapple go评论107阅读模式
英文:

How do I parse URLs in the format of /id/123 not ?foo=bar

问题

我正在尝试解析一个像这样的URL:

http://example.com/id/123

我已经阅读了net/url文档,但似乎它只能解析像这样的字符串:

http://example.com/blah?id=123

我该如何解析ID,以便在第一个示例中得到id的值?

这不是我自己的路由,而是从一个openid请求返回的http字符串。

英文:

I'm trying to parse an URL like:

http://example.com/id/123

I've read through the net/url docs but it seems like it only parses strings like

http://example.com/blah?id=123

How can I parse the ID so I end up with the value of the id in the first example?

This is not one of my own routes but a http string returned from an openid request.

答案1

得分: 16

在你的示例中,/id/123是一个路径,你可以使用路径模块的Base函数来获取"123"部分。

  1. package main
  2. import (
  3. "fmt"
  4. "path"
  5. )
  6. func main() {
  7. fmt.Println(path.Base("/id/123"))
  8. }

为了方便参考,这是路径模块的文档:http://golang.org/pkg/path/#example_Base

英文:

In your example /id/123 is a path and you can get the "123" part by using Base from the path module.

  1. package main
  2. import (
  3. "fmt"
  4. "path"
  5. )
  6. func main() {
  7. fmt.Println(path.Base("/id/123"))
  8. }

For easy reference, here's the docs on the path module. http://golang.org/pkg/path/#example_Base

答案2

得分: 8

你可以尝试使用以下正则表达式:

  1. import "regexp"
  2. re, _ := regexp.Compile("/id/(.*)")
  3. values := re.FindStringSubmatch(path)
  4. if len(values) > 0 {
  5. fmt.Println("ID : ", values[1])
  6. }

这段代码使用了正则表达式来匹配路径中的ID。如果匹配成功,它会打印出ID的值。

英文:

You can try using regular expression as follow:

  1. import "regexp"
  2. re, _ := regexp.Compile("/id/(.*)")
  3. values := re.FindStringSubmatch(path)
  4. if len(values) > 0 {
  5. fmt.Println("ID : ", values[1])
  6. }

答案3

得分: 3

以下是翻译好的内容:

这是一个适用于与您的URL具有相同结构的简单解决方案(您可以根据其他结构进行改进)。

  1. package main
  2. import (
  3. "fmt"
  4. "net/url"
  5. )
  6. var path = "http://localhost:8080/id/123"
  7. func getFirstParam(path string) (ps string) {
  8. // 忽略第一个'/',并在第二个'/'出现时获取其后的内容作为参数
  9. for i := 1; i < len(path); i++ {
  10. if path[i] == '/' {
  11. ps = path[i+1:]
  12. }
  13. }
  14. return
  15. }
  16. func main() {
  17. u, _ := url.Parse(path)
  18. fmt.Println(u.Path) // -> "/id/123"
  19. fmt.Println(getFirstParam(u.Path)) // -> "123"
  20. }

或者,如@gollipher建议的那样,使用path包:

  1. import "path"
  2. func main() {
  3. u, _ := url.Parse(path)
  4. ps := path.Base(u.Path)
  5. }

使用这种方法比正则表达式更快,前提是您事先知道所获取的URL的结构。

英文:

Here is a simple solution that works for URLs with the same structure as yours (you can improve to suit those with other structures)

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;net/url&quot;
  5. )
  6. var path = &quot;http://localhost:8080/id/123&quot;
  7. func getFirstParam(path string) (ps string) {
  8. // ignore first &#39;/&#39; and when it hits the second &#39;/&#39;
  9. // get whatever is after it as a parameter
  10. for i := 1; i &lt; len(path); i++ {
  11. if path[i] == &#39;/&#39; {
  12. ps = path[i+1:]
  13. }
  14. }
  15. return
  16. }
  17. func main() {
  18. u, _ := url.Parse(path)
  19. fmt.Println(u.Path) // -&gt; &quot;/id/123&quot;
  20. fmt.Println(getFirstParam(u.Path)) // -&gt; &quot;123&quot;
  21. }

Or, as @gollipher suggested, use the path package

  1. import &quot;path&quot;
  2. func main() {
  3. u, _ := url.Parse(path)
  4. ps := path.Base(u.Path)
  5. }

With this method it's faster than regex, provided you know before hand the structure of the URL you are getting.

huangapple
  • 本文由 发表于 2015年8月16日 13:12:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/32032050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定