urljoin

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package urljoin joins URL path segments together, normalizing slashes. It is a standard-library-only Go port of the npm "url-join" package, the small utility countless Node projects use to assemble a URL from a base and one or more path fragments without ending up with doubled or missing slashes. The problem it solves is that naive string concatenation of URL parts produces artifacts like "http://host//api" or "api/v1users" depending on which parts carried trailing or leading slashes; url-join normalizes all of that away.

URLJoin takes a variadic list of parts and returns a single joined URL. Segments are joined with exactly one "/", runs of duplicate slashes between segments are collapsed to a single slash, leading slashes are stripped from every part after the first, and trailing slashes are stripped from every part before the last. The last part keeps at most a single trailing slash, so an intentional trailing "/" survives while accidental doubles do not. Empty parts are skipped entirely, so passing "" between real segments does not introduce an empty path component.

The protocol separator is treated specially so normalization does not damage it. The "://" that follows a scheme such as "http" or "https" is preserved rather than collapsed to a single slash, and a part that is a bare protocol like "http://" is merged with the following part before joining. The "file" scheme is handled distinctly because it canonically uses three slashes: "file:///" keeps its triple slash while other schemes are normalized to the usual "://" form.

Query-string and fragment handling is likewise aware of URL structure. A slash that would otherwise sit immediately before a "?", "&", or "#" separator is removed so the query does not begin with a stray slash. When more than one part contributes a query string, the first "?" is kept as the query introducer and every subsequent "?" is rewritten to "&", so joining a base carrying "?a=1" with a fragment carrying "?b=2" yields a single well-formed "?a=1&b=2" query rather than two "?" separators.

Parity with the Node original covers the slash collapsing, protocol preservation, file-scheme triple slash, and query-combining behavior that make url-join useful. The implementation uses the same regular-expression-driven approach as the reference library, and the API is reduced to a single idiomatic variadic function, URLJoin, that returns the empty string when called with no parts.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func URLJoin

func URLJoin(parts ...string) string

URLJoin joins the given URL parts into a single URL, collapsing duplicate slashes while preserving the protocol separator and correctly combining query-string fragments.

Example

ExampleURLJoin assembles a URL from a base and path fragments, inserting exactly one slash between each part. The scheme's "://" separator is preserved rather than collapsed, so the protocol stays intact while the path segments are joined cleanly. This is the common case of building an endpoint URL from pieces without worrying about which piece carried a slash. The result is a single well-formed URL. Empty parts, if any, would simply be skipped.

package main

import (
	"fmt"

	"github.com/malcolmston/express/urljoin"
)

func main() {
	fmt.Println(urljoin.URLJoin("http://example.com", "foo", "bar"))
}
Output:
http://example.com/foo/bar
Example (Query)

ExampleURLJoin_query combines query strings from multiple parts into one well-formed query. The first "?" is kept as the query introducer and every subsequent "?" is rewritten to "&", so two parts each carrying a query merge into a single "?a=1&b=2". A slash that would otherwise sit just before the "?" is removed as well. This lets a base URL and a fragment each contribute parameters without producing two "?" separators. The result is a valid, mergeable URL.

package main

import (
	"fmt"

	"github.com/malcolmston/express/urljoin"
)

func main() {
	fmt.Println(urljoin.URLJoin("http://example.com", "foo?a=1", "?b=2"))
}
Output:
http://example.com/foo?a=1&b=2
Example (Slashes)

ExampleURLJoin_slashes shows the slash normalization that gives the package its purpose. Even though several parts carry leading or trailing slashes, the duplicates are collapsed so the output never contains a doubled slash between segments. Leading slashes are stripped from every part after the first, and trailing slashes from every part before the last. The protocol's own double slash is left untouched. The result is identical to the clean join above.

package main

import (
	"fmt"

	"github.com/malcolmston/express/urljoin"
)

func main() {
	fmt.Println(urljoin.URLJoin("http://example.com/", "/foo/", "/bar"))
}
Output:
http://example.com/foo/bar

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL