net/url package.
Parsing a URL
Parse a URL that includes scheme, authentication info, host, port, path, query params, and fragment:URL Components
Scheme
Accessing the scheme is straightforward:Authentication
User contains all authentication info:
Host and Port
TheHost contains both hostname and port. Use net.SplitHostPort to extract them:
Path and Fragment
Extract the path and fragment:Query Parameters
Raw Query String
Get query params as a raw string:Parsed Query Map
Parse query params into a map. Values are slices of strings:Query param maps are from strings to slices of strings. Index into
[0] if you only want the first value.URL Structure
Scheme - Protocol
Scheme - Protocol
The protocol (http, https, postgres, etc.)
Userinfo - Authentication
Userinfo - Authentication
Optional username and password
Host - Server location
Host - Server location
Hostname and optional port
Path - Resource path
Path - Resource path
Path to the resource
Query - Parameters
Query - Parameters
Key-value pairs after ’?’
Fragment - Anchor
Fragment - Anchor
Section identifier after ’#‘
URL Fields
The protocol scheme (http, https, etc.)
Username and password information
Hostname and port (host:port)
Path portion of the URL
Encoded query values without ’?’
Fragment identifier without ’#‘