Cookies management by TermsFeed Cookie Consent

πŸ”— URL Encode in Go

Last updated:
introduction encoding url

URL encoding, also known as percent-encoding, is a method of encoding URLs using only a limited set of characters so that the URL can be transmitted safely. A typical URL consists of the following components:

scheme://host:port/path?query

In the examples below, we will show how to encode the query part of the URL, the path part, and also how to build a full encoded URL.

Encode the query part of the URL

The url.QueryEscape() function from the net/url package is used to encode a string placed inside a URL query.

package main

import (
    "fmt"
    "net/url"
)

func main() {
    query1Val := url.QueryEscape("ab+c")
    query2Val := url.QueryEscape("de$f")

    fmt.Println(query1Val)
    fmt.Println(query2Val)
}

Output:

ab%2Bc
de%24f

To create an encoded multiple key-value query parameters string, use the url.Values structure from the net/url package.

package main

import (
    "fmt"
    "net/url"
)

func main() {
    queryValues := url.Values{}
    queryValues.Add("query", "ab+c")
    queryValues.Add("query2", "de$f")
    encodedQuery := queryValues.Encode()
    fmt.Println(encodedQuery)
}

Output:

query=ab%2Bc&query2=de%24f

Encode the path part of the URL

The url.PathEscape() function from the net/url package is used to encode a string that is placed inside a URL path segment. The path segment is encoded differently from the query, for example, the + character is allowed in the path, and should be encoded in the query.

package main

import (
    "fmt"
    "net/url"
)

func main() {
    path := url.PathEscape("foo+bar!")
    fmt.Println(path)
}

Output:

foo+bar%21

Build a full encoded URL

There are two ways to construct a full encoded URL. You can create it manually by joining different parts of the URL, where the query and path parts are escaped with the previously used functions.

package main

import (
    "fmt"
    "net/url"
)

func main() {
    // build url manually
    host := "https://example.com/"
    path := url.PathEscape("foo+bar!")
    query1Val := url.QueryEscape("ab+c")
    query2Val := url.QueryEscape("de$f")
    query := fmt.Sprintf("query=%s&query2=%s", query1Val, query2Val)
    fmt.Printf("%s %s%s?%s\n", "Manually built URL:", host, path, query)
}

Output:

Manually built URL: https://example.com/foo+bar%21?query=ab%2Bc&query2=de%24f

However, it is generally better idea to build the encoded URL by using the url.URL structure. This way is easier and less error-prone than manually constructing the resulting URL. You just need to set the Scheme, Host, Path of the URL and build the RawQuery string by encoding query parameters inside the url.Values struct.

package main

import (
    "fmt"
    "net/url"
)

func main() {
    // build url using url.URL struct
    exampleURL := &url.URL{
        Scheme: "https",
        Host:   "example.com",
        Path:   "/foo+bar!",
    }
    queryValues := url.Values{}
    queryValues.Add("query", "ab+c")
    queryValues.Add("query2", "de$f")
    exampleURL.RawQuery = queryValues.Encode()
    fmt.Printf("%s %s\n", "URL built using url.URL struct:", exampleURL)
}
URL built using url.URL struct: https://example.com/foo+bar%21?query=ab%2Bc&query2=de%24f

Thank you for being on our site 😊. If you like our tutorials and examples, please consider supporting us with a cup of coffee and we'll turn it into more great Go examples.

Have a great day!

Buy Me A Coffee

⛓️ URL Decode in Go

Learn how to URL decode a path and query parameters in Go
introduction encoding url

♾️ Infinite loop in Go

Learn how to define a “while true” loop
introduction loop

πŸ“” Convert a struct to io.Reader in Go

Learn how to convert a struct to io.Reader and send it as an HTTP POST request body
introduction http