net/url: parse URLs and escape paths
Answer: Package net/url represents a URL with scheme, host, path, query, and fragment fields. ParseRequestURI parses a raw url into a URL structure. Prefer EscapedPath over reading RawPath directly.
The URL form
The general form is [scheme:][//[userinfo@]host][/]path[?query][#fragment].
ParseRequestURI parses a raw url into a URL structure.
The Host field contains the host and port subcomponents of the URL.
If a slash does not follow the scheme, the URL is read as scheme:opaque[?query][#fragment].
A hostname and path with no scheme is invalid to parse, though ambiguities mean an error is not guaranteed.
EscapedPath, String, and Values
RawPath is optional and stores an encoded path hint.
EscapedPath returns RawPath when it is a valid escaping of Path. Otherwise it ignores RawPath and computes an escaped form on its own.
The String and RequestURI methods use EscapedPath to construct their results.
In general, call EscapedPath instead of reading u.RawPath directly.
If u.Opaque is non-empty, String uses the opaque form; otherwise it uses the host and path form.
Query parameters and form values typically go in a Values map. Those keys are case-sensitive, unlike keys in http.Header.
See also
Claims
- id: C1 claim: The Host field contains the host and port subcomponents of the URL. source: S1 evidence: "The Host field contains the host and port subcomponents of the URL"
- id: C2 claim: ParseRequestURI parses a raw url into a URL structure. source: S1 evidence: "ParseRequestURI parses a raw url into a URL structure"
- id: C3 claim: Code should call EscapedPath instead of reading RawPath directly. source: S1 evidence: "call EscapedPath instead of reading u.RawPath directly"
- id: C4 claim: Keys in a Values map are case-sensitive. source: S1 evidence: "the keys in a Values map are case-sensitive"
- id: C5 claim: If Opaque is non-empty, String uses the opaque form. source: S1 evidence: "If u.Opaque is non-empty, String uses the first form"
- id: C6 claim: EscapedPath preserves the original encoding of Path. source: S2 evidence: "the original encoding of Path"
Sources
- [S1] net/url package reference. Go. https://pkg.go.dev/net/url . License: BSD-3-Clause. Accessed 2026-11-23.
- [S2] net/url/url.go source. Go. https://go.dev/src/net/url/url.go . License: BSD-3-Clause. Accessed 2026-11-23.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go, net/url package reference; Go, net/url/url.go source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go, net/url package reference; Go, net/url/url.go source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "net/url: parse URLs and escape paths." iinventedlight Library, iil:note:cfs5w7mv, version of 2026-11-23. https://iinventedlight.com/library/notes/go-net-url/. License: CC-BY-4.0.
@misc{iil_note_cfs5w7mv,
author = {{iinventedlight}},
title = {{net/url: parse URLs and escape paths}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/go-net-url/},
note = {Permanent ID iil:note:cfs5w7mv. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- go-net-url.schema.json (schema; CC-BY-4.0)
- go-net-url.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-net-url.md.