HiveBrain v1.2.0
Get Started
← Back to all entries
patterngoCritical

Combine URL paths with path.Join()

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
pathswithurlcombinejoinpath

Problem

Is there a way in Go to combine URL paths similarly as we can do with filepaths using path.Join()?

For example see e.g. Combine absolute path and relative path to get a new absolute path.

When I use path.Join("http://foo", "bar"), I get http:/foo/bar.

See in Golang Playground.

Solution

The function path.Join expects a path, not a URL. Parse the URL to get a path and join with that path:

u, err := url.Parse("http://foo")
if err != nil { log.Fatal(err) }
u.Path = path.Join(u.Path, "bar.html")
s := u.String()
fmt.Println(s)  // prints http://foo/bar.html


Use the url.JoinPath function in Go 1.19 or later:

s, err := url.JoinPath("http://foo", "bar.html")
if err != nil { log.Fatal(err) }
fmt.Println(s) // prints http://foo/bar.html


Use ResolveReference if you are resolving a URI reference from a base URL. This operation is different from a simple path join: an absolute path in the reference replaces the entire base path; the base path is trimmed back to the last slash before the join operation.

base, err := url.Parse("http://foo/quux.html")
if err != nil {
    log.Fatal(err)
}
ref, err := url.Parse("bar.html")
if err != nil {
    log.Fatal(err)
}
u := base.ResolveReference(ref)
fmt.Println(u.String()) // prints http://foo/bar.html


Notice how quux.html in the base URL does not appear in the resolved URL.

Code Snippets

u, err := url.Parse("http://foo")
if err != nil { log.Fatal(err) }
u.Path = path.Join(u.Path, "bar.html")
s := u.String()
fmt.Println(s)  // prints http://foo/bar.html
s, err := url.JoinPath("http://foo", "bar.html")
if err != nil { log.Fatal(err) }
fmt.Println(s) // prints http://foo/bar.html
base, err := url.Parse("http://foo/quux.html")
if err != nil {
    log.Fatal(err)
}
ref, err := url.Parse("bar.html")
if err != nil {
    log.Fatal(err)
}
u := base.ResolveReference(ref)
fmt.Println(u.String()) // prints http://foo/bar.html

Context

Stack Overflow Q#34668012, score: 170

Revisions (0)

No revisions yet.