urls.JoinPath
- Syntax
- urls.JoinPath ELEMENT...
- Returns
- string
Usage
The urls.JoinPath function returns a URL created by joining the given elements and resolving any references to the current or parent directory, such as ./ or ../. The function adds a slash between elements where needed, and applies percent-encoding to characters that are not valid in a URL path. If you pass no arguments, the function returns an empty string.
If the first element is an absolute URL, the function joins the other elements to its path and keeps its query and fragment. The build fails if the first element is not a valid URL.
Unlike the path.Join function, urls.JoinPath retains consecutive leading slashes.
Arguments
ELEMENT- (
sliceorstring) One or more URL elements. If you pass a slice, the function joins each element of the slice.
Examples
{{ urls.JoinPath "a" "b" }} → a/b
{{ urls.JoinPath "/a" "b" }} → /a/b
{{ urls.JoinPath "https://example.org" "b" }} → https://example.org/b
{{ urls.JoinPath "https://example.org/a/" "b/" }} → https://example.org/a/b/
{{ urls.JoinPath "a" "b c" }} → a/b%20cElements that refer to the current or parent directory:
{{ urls.JoinPath "a" "./b" }} → a/b
{{ urls.JoinPath "a" "../b" }} → b
{{ urls.JoinPath "https://example.org/a/" "../../b" }} → https://example.org/bAn absolute URL with a query and a fragment:
{{ urls.JoinPath "https://example.org/a?x=1#y" "b" }} → https://example.org/a/b?x=1#yEmpty arguments and consecutive leading slashes:
{{ urls.JoinPath }} → "" (empty string)
{{ urls.JoinPath "" }} → /
{{ urls.JoinPath "//a" "b" }} → //a/bJoining the elements of a slice:
{{ urls.JoinPath (slice "a" "b") "c" }} → a/b/c