How to extract base url using golang

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

How to extract base url using golang

问题

给定一个URL字符串,如何仅检索基本URL(即协议://主机:端口)?

例如:

https://example.com/user/1000 => https://example.com

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()来解析它,并将结果中不需要的字段(即PathRawQueryFragment)置零。然后可以使用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(&quot;https://example.com/user/1000&quot;)
val := fmt.Sprintf(&quot;%s://%s&quot;, u.Scheme, u.Host)

The following may be more useful in the general case.

rawURL := &quot;https://user:pass@localhost:8080/user/1000/profile?p=n#abc&quot;
u, _ := url.Parse(rawURL)
psw, pswSet := u.User.Password()
for _, d := range []struct {
	actual   any
	expected any
}{
	{u.Scheme, &quot;https&quot;},
	{u.User.Username(), &quot;user&quot;},
	{psw, &quot;pass&quot;},
	{pswSet, true},
	{u.Host, &quot;localhost:8080&quot;},
	{u.Path, &quot;/user/1000/profile&quot;},
	{u.Port(), &quot;8080&quot;},
	{u.RawPath, &quot;&quot;},
	{u.RawQuery, &quot;p=n&quot;},
	{u.Fragment, &quot;abc&quot;},
	{u.RawFragment, &quot;&quot;},
	{u.RequestURI(), &quot;/user/1000/profile?p=n&quot;},
	{u.String(), rawURL},
	{fmt.Sprintf(&quot;%s://%s&quot;, u.Scheme, u.Host), &quot;https://localhost:8080&quot;},
} {
	if d.actual != d.expected {
		t.Fatalf(&quot;%s\n%s\n&quot;, d.actual, d.expected)
	}
}

<kbd>go-playground</kbd>

huangapple
  • 本文由 发表于 2022年5月26日 13:57:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/72387330.html
匿名

发表评论

匿名网友

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

确定