URL encoding is essential when a URL contains characters that might have special meaning in the context of an URL such as spaces, ampersands, question marks, and more.
To ensure that such characters are transmitted as is, the URL encoding provides us with a way of representing them into values that can be read by either the client side or the server-side applications.
In this tutorial, we will learn how to perform the URL encoding and decoding using the net/URL package provided in the Go standard library.
Intro to URL Encoding/Decoding
URL encoding allows us to replace the special characters with percent-encoded representations to ensure that they are correctly interpreted by the web servers and browsers.
For example, URL encoding converts the following special characters into their URL-friendly representation:
- Space (‘ ‘) is encoded as “%20”
- Ampersand (‘&’) is encoded as “%26”
- Question mark (‘?’) is encoded as “%3F”
URL decoding, on the other hand, is the process of reversing the URL encoding. It takes a URL-encoded string and converts it back to its original form.
Golang URL Encoding
Let us start with the basics and cover the various methods and techniques that we can use to perform the URL encoding in Go.
Method 1: Using Url.QueryEscape
The first method that we can use is the “QueryEscape” function from the net/URL package. This is the simplest and most common method of performing the URL encoding in Go.
The function takes a string and returns a URL-encoded version of that string as shown in the following example code:
import (
"fmt"
"net/url"
)
func main() {
input := "https://demo.elastic.co/cookie/index.html?goto=/app/apm&action=none"
encoded := url.QueryEscape(input)
fmt.Println(encoded)
}
In this example, we use the “QueryEscape” function to convert the URL from of https://demo.elastic.co/cookie/index.html?goto=/app/apm&action=none.
to:
You will notice that in the encoded string, all the special characters are converted to their URL safe equivalent.
Method 2: Using Url.Values
We can also use the “url.Values” function in the net/URL package to encode the multiple key-value pairs as query strings.
The function automatically performs the URL encoding for each key and value.
import (
"fmt"
"net/url"
)
func main() {
values := url.Values{}
values.Add("goto", "/app/apm")
values.Add("action", "none")
encoded := values.Encode()
fmt.Println(encoded)
}
In this example, we use the url.Values() function to encode the key-value pairs namely “goto” and “action” into a string.
Golang URL Decoding
URL decoding is also a common task especially when implementing an HTTP client or server in Go.
The following are some of the common tasks that you can use in URL decoding.
Method 1: Using Url.QueryUnescape
Yes, there is such a function. This function is the opposite of the QueryEscape which allows us to encode the URL.
The function takes a URL-encoded string and returns the decoded version as demonstrated in the following example:
import (
"fmt"
"net/url"
)
func main() {
encoded := "https%3A%2F%2Fdemo.elastic.co%2Fcookie%2Findex.html%3
Fgoto%3D%2Fapp%2Fapm%26action%3Dnone
decoded, err := url.QueryUnescape(encoded)
if err != nil {
fmt.Println("Error decoding:", err)
return
}
fmt.Println(decoded)
}
This should return the decoded URL string as follows:
https://demo.elastic.co/cookie/index.html?goto=/app/apm&action=none
Method 2: Using Url.Values
If we have a query string and we want to decode it into the key-value pairs, we can use the “url.Values” type along with the “url.ParseQuery” function.
import (
"fmt"
"net/url"
)
func main() {
queryString := "action=none&goto=%2Fapp%2Fapm"
values, err := url.ParseQuery(queryString)
if err != nil {
fmt.Println("Error parsing query:", err)
return
}
action := values.Get("action")
goto_ := values.Get("goto")
fmt.Println("action:", action)
fmt.Println("goto:", goto_)
}
Output String:
goto: /app/apm
Conclusion
In this tutorial, we learned all about using the net/URL package in the Go ecosystem to perform the URL encoding and decoding as efficiently as possible.