url.Parse
u, _ := url.Parse("https://user:pw@example.com:8080/a/b?x=1&y=2#frag")
fmt.Println(u.Scheme, u.Host, u.Path, u.RawQuery, u.Fragment)
fmt.Println(u.User.Username())
Output
https example.com:8080 /a/b x=1&y=2 frag
user
Build from parts
u := &url.URL{
Scheme: "https",
Host: "example.com",
Path: "/search",
RawQuery: url.Values{"q": {"go maps"}, "n": {"20"}}.Encode(),
}
fmt.Println(u.String())
Output
https://example.com/search?n=20&q=go+maps
Resolve a relative reference
ResolveReference does what a browser does when following a relative link.
base, _ := url.Parse("https://a.com/docs/")
rel, _ := url.Parse("../x.html")
fmt.Println(base.ResolveReference(rel))
Output
https://a.com/x.html