Developer Documentation
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
1values := url.Values{}2values.Set("q", "café & tea")3values.Set("page", "1")45query := values.Encode()6fmt.Println(query)

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Path segments#

Go
1segment := url.PathEscape("report 2026.pdf")2restored, err := url.PathUnescape(segment)3if err != nil { return err }4fmt.Println(segment, restored)

Important Notes#

url.QueryEscape/Values.Encode and url.PathEscape have different semantics. Choose the function that matches the URL component.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement URL Encoding in other languages: