英文:
How to extract base url using golang
问题
给定一个URL字符串,如何仅检索基本URL(即协议://主机:端口)?
例如:
https://localhost:8080/user/1000/profile => https://localhost:8080
我尝试使用url.Parse()解析URL,但是net/url似乎没有返回基本URL的方法。我可以尝试将URL的各个部分连接起来以获取基本URL,但我只是想检查是否有更好的替代方法。
英文:
Given a url string, how to retrieve only the base url (i.e. protocol://host:port)
For example
> https://example.com/user/1000 => https://example.com
> https://localhost:8080/user/1000/profile => https://localhost:8080
I've tried parsing the url with url.Parse() but the net/url doesn't seem to have a method that returns the base url. I could try appending the individual parts of url to get the base url but I just wanted to check if there are any better alternatives to this.
答案1
得分: 4
我会使用url.Parse()来解析它,并将结果中不需要的字段(即Path、RawQuery和Fragment)置零。然后可以使用URL.String()获取结果(基本URL)。
例如:
u, err := url.Parse("https://user@pass:localhost:8080/user/1000/profile?p=n#abc")
if err != nil {
panic(err)
}
fmt.Println(u)
u.Path = ""
u.RawQuery = ""
u.Fragment = ""
fmt.Println(u)
fmt.Println(u.String())
这将输出(在Go Playground上尝试):
https://user@pass:localhost:8080/user/1000/profile?p=n#abc
https://user@pass:localhost:8080
https://user@pass:localhost:8080
英文:
I would parse it using url.Parse(), and zero the fields you don't want in the result, namely Path, RawQuery and Fragment. Then the result (base URL) can be acquired using URL.String().
For example:
u, err := url.Parse("https://user@pass:localhost:8080/user/1000/profile?p=n#abc")
if err != nil {
panic(err)
}
fmt.Println(u)
u.Path = ""
u.RawQuery = ""
u.Fragment = ""
fmt.Println(u)
fmt.Println(u.String())
This will output (try it on the Go Playground):
https://user@pass:localhost:8080/user/1000/profile?p=n#abc
https://user@pass:localhost:8080
https://user@pass:localhost:8080
答案2
得分: 2
你可以尝试以下代码:
u, _ := url.Parse("https://example.com/user/1000")
val := fmt.Sprintf("%s://%s", u.Scheme, u.Host)
在一般情况下,以下代码可能更有用:
rawURL := "https://user:pass@localhost:8080/user/1000/profile?p=n#abc"
u, _ := url.Parse(rawURL)
psw, pswSet := u.User.Password()
for _, d := range []struct {
actual any
expected any
}{
{u.Scheme, "https"},
{u.User.Username(), "user"},
{psw, "pass"},
{pswSet, true},
{u.Host, "localhost:8080"},
{u.Path, "/user/1000/profile"},
{u.Port(), "8080"},
{u.RawPath, ""},
{u.RawQuery, "p=n"},
{u.Fragment, "abc"},
{u.RawFragment, ""},
{u.RequestURI(), "/user/1000/profile?p=n"},
{u.String(), rawURL},
{fmt.Sprintf("%s://%s", u.Scheme, u.Host), "https://localhost:8080"},
}{
if d.actual != d.expected {
t.Fatalf("%s\n%s\n", d.actual, d.expected)
}
}
<kbd>go-playground</kbd>
英文:
You can try
u, _ := url.Parse("https://example.com/user/1000")
val := fmt.Sprintf("%s://%s", u.Scheme, u.Host)
The following may be more useful in the general case.
rawURL := "https://user:pass@localhost:8080/user/1000/profile?p=n#abc"
u, _ := url.Parse(rawURL)
psw, pswSet := u.User.Password()
for _, d := range []struct {
actual any
expected any
}{
{u.Scheme, "https"},
{u.User.Username(), "user"},
{psw, "pass"},
{pswSet, true},
{u.Host, "localhost:8080"},
{u.Path, "/user/1000/profile"},
{u.Port(), "8080"},
{u.RawPath, ""},
{u.RawQuery, "p=n"},
{u.Fragment, "abc"},
{u.RawFragment, ""},
{u.RequestURI(), "/user/1000/profile?p=n"},
{u.String(), rawURL},
{fmt.Sprintf("%s://%s", u.Scheme, u.Host), "https://localhost:8080"},
} {
if d.actual != d.expected {
t.Fatalf("%s\n%s\n", d.actual, d.expected)
}
}
<kbd>go-playground</kbd>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论