To convert a URL-encoded string to its decoded form using strconv
in Golang, you can use the Unquote
function from the strconv
package.
Here is an example of how you can do it:
package main
import (
"fmt"
"net/url"
"strconv"
)
func main() {
// URL-encoded string
encodedString := "%2Fhello%2Fworld%3Ffoo%3Dbar%26baz%3Dqux"
// Decoding the URL-encoded string
decodedString, err := url.QueryUnescape(encodedString)
if err != nil {
fmt.Println("Error while decoding:", err)
return
}
// Unquoting the URL-encoded string (if needed)
unquotedString, err := strconv.Unquote(decodedString)
if err != nil {
// If the decoded string was not surrounded by double quotes, it will return the same decoded string
unquotedString = decodedString
}
fmt.Println("Decoded string:", unquotedString)
}
In this example, the url.QueryUnescape
function is used to decode the URL-encoded string. Then, the strconv.Unquote
function is used to unquote the decoded string, if it is surrounded by double quotes.