DevToolsForYou

URL Encoder and Decoder in Go — Code Examples

URL Encoder and Decoder in GoUse the online tool →

URL encoding (percent-encoding) replaces characters that are not allowed in URLs with a percent sign followed by their hexadecimal value. Here is how to encode and decode URL components in each language.

Go's net/url package provides url.QueryEscape for query parameters and url.PathEscape for path segments.

Go
package main

import (
    "fmt"
    "net/url"
)

func main() {
    input := "hello world & foo=bar"

    // Encode a query parameter value (spaces become +)
    encoded := url.QueryEscape(input)
    fmt.Println(encoded) // hello+world+%26+foo%3Dbar

    // Decode
    decoded, err := url.QueryUnescape("hello+world+%26+foo%3Dbar")
    if err != nil {
        panic(err)
    }
    fmt.Println(decoded) // hello world & foo=bar

    // Encode a path segment (spaces become %20, / is encoded)
    pathEncoded := url.PathEscape(input)
    fmt.Println(pathEncoded) // hello%20world%20&%20foo=bar

    // Build a query string
    params := url.Values{}
    params.Set("q", "hello world")
    params.Set("page", "1")
    fmt.Println(params.Encode()) // page=1&q=hello+world
}
Notes & gotchas
  • QueryEscape encodes spaces as + (for query strings); PathEscape encodes them as %20 (for URL paths).
  • url.Values.Encode() builds a sorted, encoded query string.
  • Always handle the error from QueryUnescape — malformed percent sequences return an error.
Try it in your browser

Need to url encode/decode without writing code? The URL Encoder and Decoder runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open URL Encode/Decode
URL Encoder and Decoder in other languages