<p>The net/url package only seems to be able to parse the query string in the format of ?foo=bar</p>
<hr/>**评论:**<br/><br/>SteazGaming: <pre><p>it's not canonical, so you may have to write your own parser: <code>canonical urls: scheme://[userinfo@]host/path[?query][#fragment]</code></p>
<p>or you can parse the url with net/url and write your own parser for just the URL.Path and decode it to whatever data structure you want from there.</p>
<p>edit: here's a quick playground: <a href="http://play.golang.org/p/qLag2qXR99">http://play.golang.org/p/qLag2qXR99</a></p></pre>media_guru: <pre><p>I use httprouter: <a href="https://github.com/julienschmidt/httprouter" rel="nofollow">https://github.com/julienschmidt/httprouter</a></p></pre>daveddev: <pre><p>Try this out. Please let me know if anything needs correction/improvement.</p>
<p><a href="https://github.com/codemodus/parth" rel="nofollow">https://github.com/codemodus/parth</a></p>
<p>Edited to add: Benchmarks are provided comparing available functions with the typical alternatives. And... path.Base() is just slightly faster for looking up the final segment (42ns vs 51ns in parth). Parth would be useful in the case that you would like to leave the string to (whatever) type parsing out of your code, and/or in the case that you need to lookup by positive segment indexes or negative segment indexes lower than -1.</p></pre>Incubuss: <pre><p>Have a look at Gorilla mux: <a href="http://www.gorillatoolkit.org/pkg/mux" rel="nofollow">http://www.gorillatoolkit.org/pkg/mux</a></p></pre>Gacnt: <pre><p>This isn't a route though. Like I'm not setting the route for my own get request</p>
<p>It's a returned url string from an openid request </p></pre>jamra06: <pre><p>strings.Split</p></pre>Incubuss: <pre><p>Ah, I see.</p>
<p>Perhaps use Base from the path package if your urls are simple enough: <a href="http://golang.org/pkg/path/#example_Base" rel="nofollow">http://golang.org/pkg/path/#example_Base</a></p></pre>Gacnt: <pre><p>This just might be the answer thanks,</p>
<p>The string returned is in the format of:</p>
<p><a href="http://steamcommunity.com/openid/id/" rel="nofollow">http://steamcommunity.com/openid/id/</a><steamid> so I need to get the steamid so I can make other API calls. </p></pre>jerf: <pre><p>Zigging instead of zagging, I might suggest:</p>
<pre><code>base := "http://steamcommunity.com/openid/id/"
if strings.HasPrefix(url, base) {
return url[len(base):], nil
} else {
return "", errors.New("Unexpected ID format in return")
}
</code></pre>
<p>Morally speaking, you're basically OK treating the prefix as an opaque blob that you choose not to understand, and as long as you see it, just chopping out the ID you want. You're safe with that because you'll check to make sure that the correct blob is there. This technique is virtually guaranteed to be a bug without that HasPrefix. (Which I may have backwards, I'm not looking it up.)</p>
<p>The reality is that if the URL that you get back changes in any way at all, you want the error, not code that unconditionally just takes the last bit.</p></pre>elithrar_: <pre><p>Yep, almost definitely <code>path.Base</code> then. More complicated cases can use <code>strings.Split</code> (and its variants) and manipulate the <code>[]string</code> returned.</p></pre>IntellectualReserve: <pre><p>If you could provide a more detailed example case, you might get better answers.</p>
<p>It sounds to me like you're getting a URL in a http response body and you'd like to know how to parse out the path from the domain.</p>
<p>You could strings.Split() the string by the '/' character, and then find the slice you care about. Or if you're just trying to get the path without the domain, you could slice out the domain and rejoin the slices with strings.Join()</p>
<p>That help?</p></pre>egonelbre: <pre><p><a href="https://play.golang.org/p/Y-qeGzy15q" rel="nofollow">https://play.golang.org/p/Y-qeGzy15q</a></p></pre>uabassguy: <pre><p>Gorilla mux handles this pretty well</p></pre>echophant: <pre><p>In <a href="http://golang.org/pkg/net/http/#ServeMux" rel="nofollow">the documentation for net/http</a>, any pattern that ends with "/" matches all sub-paths. So in your case, you'd do something like:</p>
<pre><code>http.HandleFunc("/id/", handlerFunc)
</code></pre>
<p>and then handlerFunc would look do parsing like specified in other comments. However, if you need something more sophisticated, you can do something like:</p>
<pre><code>type Path string
func (p *Path) ID() int64 {
v := strings.Split(string(p), "/")
if len(v) > 1 {
if i, err := strconv.ParseInt(v[1], 10, 64); err == nil {
return i
} else {
return 0
}
return 0
}
</code></pre>
<p>And then your handlerFunc would contain:</p>
<pre><code>Path(url.Path).ID()
</code></pre>
<p>No need to drag in an external dependency for something so straight forward.</p></pre>Gacnt: <pre><p>I stated previously that this is not a route. Its a string returned from openid</p></pre>jayrox: <pre><p>you worded your question as if its a route and not a string. if its a string, just split it.</p></pre>Gacnt: <pre><p>It literally says in my question "This is not a route"</p></pre>jayrox: <pre><p>"How can I parse an URL in the format of /id/123 not ?id=123"</p></pre>Gacnt: <pre><p>Now keep reading the rest of the question lol. </p></pre>jayrox: <pre><p>i did, was just pointing out where the logical break that causes answers the be given for the wrong/incomplete question you are asking. people tend to read up to the point where information is given and stop when the little footnote-notes are added. what i am saying is, for a number of people, they may have stopped reading when they saw the "()"</p></pre>Gacnt: <pre><p>Fair enough!</p></pre>izuriel: <pre><p>Just pointing out the logic is very similar if not exactly the same between the two processes. </p></pre>minaguib: <pre><p>Split on / and zip odd-even elements into a map ?</p></pre>expressadmin: <pre><p>Regex? <a href="https://golang.org/pkg/regexp/" rel="nofollow">https://golang.org/pkg/regexp/</a></p></pre>shyym: <pre><p>I used to have a problem and I used regex to solve it.</p>
<p>Now I have 2 problems :)</p></pre>expressadmin: <pre><p>Regex... the computer equivalent to black voodoo.</p></pre>earthboundkid: <pre><p>Sometimes, but this case is pretty much best solved with a regex: here's a string, if it matches some pattern give me the digits in the end of it. </p></pre>
How can I parse an URL in the format of /id/123 not ?id=123 (this is not a route but a string returned from an API request e.g. example.com/id/123)
polaris · · 822 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传