net/url

Guided tour · Networking · pkg.go.dev →

Parse, build, and escape URLs. Handles query strings, paths, and userinfo correctly.

Parse and rebuild

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

Query values

url.Values — repeated-key map

v := url.Values{}
v.Set("name", "ada")
v.Add("tag", "a")
v.Add("tag", "b")
fmt.Println(v.Encode())
Output
name=ada&tag=a&tag=b

Escaping

QueryEscape / PathEscape

They differ: QueryEscape encodes spaces as '+', PathEscape as '%20'. Use the right one for where the value will live.

url.QueryEscape("a b/c?")   // "a+b%2Fc%3F"
url.PathEscape("a b/c?")    // "a%20b%2Fc%3F"