2 Ways to Encode URL String in Golang

URL encoding is a method used by web servers and browsers to convert unreadable or special characters to a generally acceptable format.

The URL encoding makes URLs that contain invalid characters readable by both the server and the client.

Encoding can be applied to Uniform Resource Names (URNs), Uniform Resource Identifiers (URIs), and Uniform Resource Locators (URLs), and selected characters in the URL are replaced by one or more character triplets made up of the percent character and two hexadecimal numbers.

Why use URL encoding in Go

URL encoding is useful in Go because we can ensure that special characters in a string are adequately represented when used as a component in a URL.

URL encoding is used in different URL parts, such as a path and query parameters.

Golang encode url string

There are two ways to encode a URL string in Go.

  1. Using QueryEscape(): It encodes a string to be safely placed inside a URL query string.
  2. Using PathEscape(): It encodes a string to be safely placed inside a URL path segment.

Method 1: Using QueryEscape()

Golang QueryEscape() is the “net/url” package’s function that escapes a string properly in a URL query.

The QueryEscape() function replaces special characters in the input string with their corresponding percent-encoded values, making the string safe to use as a component in a URL query.

Let’s encode the URL string step by step.

Step 1: Import the “net/url” package

To use the QueryEscape() function, you need to import the “net/url” package in your Go file.

package main

import (
  "fmt"
  "net/url"
)

Step 2: Define a URL to be encoded

Create a URL string and assign it to a variable.

package main

import (
  "fmt"
  "net/url"
)

func main() {
  URLString := "https://askgolang.com"
}

Step 3: Use the QueryEscape() function

The QueryEscape() function accepts a URL string to be encoded and prints it using the fmt.Println() function.

package main

import (
  "fmt"
  "net/url"
)

func main() {
  URLString := "https://askgolang.com"
  encodedStr := url.QueryEscape(URLString)
  fmt.Println(encodedStr)
}
Output
https%3A%2F%2Faskgolang.com

And we got the encoded string URL.

Method 2: Using PathEscape()

Golang PathEscape() function escapes a string for use in a URL path.

The PathEscape() function replaces special characters in the input string with their corresponding percent-encoded values, making the string safe to use as a component in a URL path.

Let’s escape a URL string using the url.PathEscape() function step by step.

Step 1: Import the “net/url” package

To use the PathEscape() function in the Go script, you must import the “net/url” package.

import (
  "net/url"
)

Step 2: Define a URL that needs to be encoded

Create a URL string and assign it to a variable.

URLString := "https://askgolang.com"

Step 3: Use the PathEscape() function

The PathEscape() function accepts a URL string to be encoded and prints it using the fmt.Println() function.

package main

import (
  "fmt"
  "net/url"
)

func main() {
  URLString := "https://askgolang.com"
  encodedStr := url.PathEscape(URLString)
  fmt.Println(encodedStr)
}
Output
https:%2F%2Faskgolang.com

Conclusion

Use the url.QueryEscape() or url.PathEscape() function to encode a URL string in Golang.

Both functions accept the URL string and return the encoded urls.

You can avoid worrying about manually escaping special characters in URLs by utilizing the QueryEscape(), and PathEscape() functions found in the net/url package of the Go standard library.

Leave a Comment