Go
Updated September 19, 2026
URL Encoding and Decoding in Go
Percent-encode URL components correctly and distinguish path, query, and form encoding in Go.
URL encoding is context-sensitive. A path segment, a query value, and application/x-www-form-urlencoded data do not use exactly the same escaping rules, so prefer APIs designed for the specific component you are building.
Quick Start#
The following example uses the standard Go API or a widely used package for this task.
Query values#
Go
Common Operations#
This second pattern covers a common production operation or a safer way to handle real input.
Path segments#
Go
Important Notes#
url.QueryEscape/Values.Encode and url.PathEscape have different semantics. Choose the function that matches the URL component.
Best Practices#
- Treat external input as untrusted and handle parse/validation errors explicitly.
- Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
- Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.