SoFunction
Updated on 2025-03-05

Detailed explanation of urlencode and urldecode encoding and decoding in Golang

1. urlencode

Under golang, you can use the net/url module to implement urlencode and urldecode operations. The specific implemented functions are sum, the code is as follows:

package main
import(    "fmt"
    "net/url")
func main()  {    var urlStr string = "Road to Operation and Maintenance"
    escapeUrl := (urlStr)
    ("coding:",escapeUrl)
    enEscapeUrl, _ := (escapeUrl)
    ("decoding:",enEscapeUrl)
}

If multiple parameters are transcoding are involved, this is available in both get and post requests, such as id=100&site=. You can transcode after adding it and send it:

package main
import (
	"fmt"
	"net/url"
)
func main() {
	params := {}
	("name", "@Rajeev")
	("phone", "+919999999999")
	(())
}
#Output:name=%40Rajeev&phone=%2B919999999999

Similarly, after we process operations through other functions of the url, the URL of a typical get request is as follows:

package main
import (
	"fmt"
	"net/url"
)
func main() {
	// Let's start with a base url
	baseUrl, err := ("")
	if err != nil {
		("Malformed URL: ", ())
		return
	}
	// Add a Path Segment (Path segment is automatically escaped)
	 += "path with?reserved characters"
	// Prepare Query Parameters
	params := {}
	("q", "Hello World")
	("u", "@rajeev")
	// Add Query Parameters to the URL
	 = () // Escape Query Parameters
	("Encoded URL is %q
", ())
}
#Output resultEncoded URL is "/path%20with%3Freserved%20characters?q=Hello+World&u=%40rajeev"

2. urldecode

For simple url decoding, just use Unescape directly, as follows:

package main
import (
	"fmt"
	"log"
	"net/url"
)
func main() {
	encodedValue := "Hell%C3%B6+W%C3%B6rld%40Golang"
	decodedValue, err := (encodedValue)
	if err != nil {
		(err)
		return
	}
	(decodedValue)
}

With form parameter, as mentioned above, in the format "param1=value1¶m2=value2" format, you can use the() parameter to parse it into map[string][]string format, as follows:

package main
import (
	"fmt"
	"log"
	"net/url"
)
func main() {
	queryStr := "name=Rajeev%20Singh&phone=%2B9199999999&phone=%2B628888888888"
	params, err := (queryStr)
	if err != nil {
		(err)
		return
	}
	("Query Params: ")
	for key, value := range params {
		("  %v = %v
", key, value)
	}
}
#The output is:Query Params:
  name = [Rajeev Singh]
  phone = [+9199999999 +628888888888]

A get request class with a URL with parameters after it can be parsed and decoded in the following way

package main
import (
	"fmt"
	"log"
	"net/url"
)
func main() {
	u, err := ("/person?name=Rajeev%20Singh&phone=%2B919999999999&phone=%2B628888888888")
	if err != nil {
		(err)
		return
	}
	("Scheme: ", )
	("Host: ", )
	queries := ()
	("Query Strings: ")
	for key, value := range queries {
		("  %v = %v
", key, value)
	}
	("Path: ", )
}
# The output is as follows:Scheme:  https
Host:  
Query Strings:
  phone = [+919999999999 +628888888888]
  name = [Rajeev Singh]
Path:  /person

Summarize

This is the article about urlencode and urldecode encoding and decoding in Golang. For more related urlencode and urldecode content in Golang, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!