std/net/url
import "github.com/eugenioenko/goalchemy/std/net/url"Package url parses URLs and implements query escaping, following Go’s net/url: the same parsing rules, escaping and error messages. Bracketed IPv6 hosts are validated as net/netip does. Behavior matches Go’s defaults for a module declaring go 1.25: urlstrictcolons=0, so hosts with several colons are accepted, and ParseQuery accepts at most 10000 parameters.
JoinPath
Section titled “JoinPath”func JoinPath(base string, elem ...string) (result string, err error)JoinPath returns a URL string with the provided path elements joined to the existing path of base and the resulting path cleaned of any ./ or ../ elements. Path elements must already be in escaped form, as produced by PathEscape.
PathEscape
Section titled “PathEscape”func PathEscape(s string) stringPathEscape escapes the string so it can be safely placed inside a URL path segment, replacing special characters (including /) with %XX sequences as needed.
PathUnescape
Section titled “PathUnescape”func PathUnescape(s string) (string, error)PathUnescape does the inverse transformation of PathEscape, converting each 3-byte encoded substring of the form “%AB” into the hex-decoded byte 0xAB. It returns an error if any % is not followed by two hexadecimal digits. PathUnescape is identical to QueryUnescape except that it does not unescape ‘+’ to ’ ’ (space).
QueryEscape
Section titled “QueryEscape”func QueryEscape(s string) stringQueryEscape escapes the string so it can be safely placed inside a URL query.
QueryUnescape
Section titled “QueryUnescape”func QueryUnescape(s string) (string, error)QueryUnescape does the inverse transformation of QueryEscape, converting each 3-byte encoded substring of the form “%AB” into the hex-decoded byte 0xAB. It returns an error if any % is not followed by two hexadecimal digits.
type Error
Section titled “type Error”type Error struct { Op string URL string Err error}Error reports an error and the operation and URL that caused it.
Error.Error
Section titled “Error.Error”func (e *Error) Error() stringError.Temporary
Section titled “Error.Temporary”func (e *Error) Temporary() boolTemporary reports whether Err reports a temporary condition.
Error.Timeout
Section titled “Error.Timeout”func (e *Error) Timeout() boolTimeout reports whether Err reports a timeout.
Error.Unwrap
Section titled “Error.Unwrap”func (e *Error) Unwrap() errortype EscapeError
Section titled “type EscapeError”type EscapeError stringEscapeError reports an invalid %-escape.
EscapeError.Error
Section titled “EscapeError.Error”func (e EscapeError) Error() stringtype InvalidHostError
Section titled “type InvalidHostError”type InvalidHostError stringInvalidHostError reports an invalid character in a host name.
InvalidHostError.Error
Section titled “InvalidHostError.Error”func (e InvalidHostError) Error() stringtype URL
Section titled “type URL”type URL struct { Scheme string Opaque string User *Userinfo Host string Path string Fragment string RawQuery string RawPath string RawFragment string ForceQuery bool OmitHost bool}A URL represents a parsed URL (technically, a URI reference). The general form represented is: [scheme:][//[userinfo@]host][/]path[?query][#fragment] URLs that do not start with a slash after the scheme are interpreted as: scheme:opaque[?query][#fragment] Path and Fragment are stored in decoded form; RawPath and RawFragment hold their original encoding when it differs from the default one.
func Parse(rawURL string) (*URL, error)Parse parses a raw url into a URL structure. The url may be relative (a path, without a host) or absolute (starting with a scheme). Trying to parse a hostname and path without a scheme is invalid but may not necessarily return an error, due to parsing ambiguities.
ParseRequestURI
Section titled “ParseRequestURI”func ParseRequestURI(rawURL string) (*URL, error)ParseRequestURI parses a raw url into a URL structure. It assumes that url was received in an HTTP request, so the url is interpreted only as an absolute URI or an absolute path. The string url is assumed not to have a #fragment suffix.
URL.AppendBinary
Section titled “URL.AppendBinary”func (u *URL) AppendBinary(b []byte) ([]byte, error)AppendBinary appends the String form of u to b.
URL.Clone
Section titled “URL.Clone”func (u *URL) Clone() *URLClone creates a deep copy of the fields of the subject URL.
URL.EscapedFragment
Section titled “URL.EscapedFragment”func (u *URL) EscapedFragment() stringEscapedFragment returns the escaped form of u.Fragment. In general there are multiple possible escaped forms of any fragment. EscapedFragment returns u.RawFragment when it is a valid escaping of u.Fragment. Otherwise EscapedFragment ignores u.RawFragment and computes an escaped form on its own.
URL.EscapedPath
Section titled “URL.EscapedPath”func (u *URL) EscapedPath() stringEscapedPath returns the escaped form of u.Path. In general there are multiple possible escaped forms of any path. EscapedPath returns u.RawPath when it is a valid escaping of u.Path. Otherwise EscapedPath ignores u.RawPath and computes an escaped form on its own.
URL.Hostname
Section titled “URL.Hostname”func (u *URL) Hostname() stringHostname returns u.Host, stripping any valid port number if present. If the result is enclosed in square brackets, as literal IPv6 addresses are, the square brackets are removed from the result.
URL.IsAbs
Section titled “URL.IsAbs”func (u *URL) IsAbs() boolIsAbs reports whether the URL is absolute. Absolute means that it has a non-empty scheme.
URL.JoinPath
Section titled “URL.JoinPath”func (u *URL) JoinPath(elem ...string) *URLJoinPath returns a new URL with the provided path elements joined to any existing path and the resulting path cleaned of any ./ or ../ elements. Any sequences of multiple / characters will be reduced to a single /. Path elements must already be in escaped form, as produced by PathEscape.
URL.MarshalBinary
Section titled “URL.MarshalBinary”func (u *URL) MarshalBinary() (text []byte, err error)MarshalBinary returns the String form of u.
URL.Parse
Section titled “URL.Parse”func (u *URL) Parse(ref string) (*URL, error)Parse parses a URL in the context of the receiver. The provided URL may be relative or absolute. Parse returns nil, err on parse failure, otherwise its return value is the same as ResolveReference.
URL.Port
Section titled “URL.Port”func (u *URL) Port() stringPort returns the port part of u.Host, without the leading colon. If u.Host doesn’t contain a valid numeric port, Port returns an empty string.
URL.Query
Section titled “URL.Query”func (u *URL) Query() ValuesQuery parses RawQuery and returns the corresponding values. It silently discards malformed value pairs. To check errors use ParseQuery.
URL.Redacted
Section titled “URL.Redacted”func (u *URL) Redacted() stringRedacted is like String but replaces any password with “xxxxx”. Only the password in u.User is redacted.
URL.RequestURI
Section titled “URL.RequestURI”func (u *URL) RequestURI() stringRequestURI returns the encoded path?query or opaque?query string that would be used in an HTTP request for u.
URL.ResolveReference
Section titled “URL.ResolveReference”func (u *URL) ResolveReference(ref *URL) *URLResolveReference resolves a URI reference to an absolute URI from an absolute base URI u, per RFC 3986 Section 5.2. The URI reference may be relative or absolute. ResolveReference always returns a new URL instance, even if the returned URL is identical to either the base or reference. If ref is an absolute URL, then ResolveReference ignores base and returns a copy of ref.
URL.String
Section titled “URL.String”func (u *URL) String() stringString reassembles the URL into a valid URL string. The general form of the result is one of: scheme:opaque?query#fragment scheme://userinfo@host/path?query#fragment If u.Opaque is non-empty, String uses the first form; otherwise it uses the second form. Any non-ASCII characters in host are escaped. To obtain the path, String uses u.EscapedPath().
URL.UnmarshalBinary
Section titled “URL.UnmarshalBinary”func (u *URL) UnmarshalBinary(text []byte) errorUnmarshalBinary parses text with Parse and stores the result in u.
type Userinfo
Section titled “type Userinfo”type Userinfo struct {}The Userinfo type is an immutable encapsulation of username and password details for a URL.
func User(username string) *UserinfoUser returns a Userinfo containing the provided username and no password set.
UserPassword
Section titled “UserPassword”func UserPassword(username, password string) *UserinfoUserPassword returns a Userinfo containing the provided username and password.
Userinfo.Password
Section titled “Userinfo.Password”func (u *Userinfo) Password() (string, bool)Password returns the password in case it is set, and whether it is set.
Userinfo.String
Section titled “Userinfo.String”func (u *Userinfo) String() stringString returns the encoded userinfo information in the standard form of “username[:password]”.
Userinfo.Username
Section titled “Userinfo.Username”func (u *Userinfo) Username() stringUsername returns the username.
type Values
Section titled “type Values”type Values map[string][]stringValues maps a string key to a list of values. It is typically used for query parameters and form values. The keys in a Values map are case-sensitive.
ParseQuery
Section titled “ParseQuery”func ParseQuery(query string) (Values, error)ParseQuery parses the URL-encoded query string and returns a map listing the values specified for each key. ParseQuery always returns a non-nil map containing all the valid query parameters found; err describes the first decoding error encountered, if any. Query is expected to be a list of key=value settings separated by ampersands. A setting without an equals sign is interpreted as a key set to an empty value. Settings containing a non-URL-encoded semicolon are considered invalid. More than 10000 settings are rejected.
Values.Add
Section titled “Values.Add”func (v Values) Add(key, value string)Add adds the value to key. It appends to any existing values associated with key.
Values.Clone
Section titled “Values.Clone”func (vs Values) Clone() ValuesClone creates a deep copy of the subject Values.
Values.Del
Section titled “Values.Del”func (v Values) Del(key string)Del deletes the values associated with key.
Values.Encode
Section titled “Values.Encode”func (v Values) Encode() stringEncode encodes the values into “URL encoded” form (“bar=baz&foo=quux”) sorted by key.
Values.Get
Section titled “Values.Get”func (v Values) Get(key string) stringGet gets the first value associated with the given key. If there are no values associated with the key, Get returns the empty string. To access multiple values, use the map directly.
Values.Has
Section titled “Values.Has”func (v Values) Has(key string) boolHas checks whether a given key is set.
Values.Set
Section titled “Values.Set”func (v Values) Set(key, value string)Set sets the key to value. It replaces any existing values.