package main
import (
"fmt"
"net/url"
)
// decodeQueryString decodes a URL-encoded string.
// It automatically handles percent-encoding (%xx) and '+' to space conversion.
func decodeQueryString(encodedStr string) (string, error) {
decoded, err := url.QueryUnescape(encodedStr)
if err != nil {
return "", err
}
return decoded, nil
}
func main() {
// Example 1: Standard URL encoding with special characters
sample1 := "Hello%20World%21%20Go%20%26%20Web"
fmt.Println("--- Example 1: Standard URL Encoding ---")
fmt.Println("Encoded:", sample1)
decoded1, err := decodeQueryString(sample1)
if err != nil {
fmt.Printf("Error decoding: %v\n", err)
} else {
fmt.Println("Decoded:", decoded1)
}
// Example 2: Form query string encoding (spaces represented as '+')
sample2 := "Jane+Doe+from+New+York"
fmt.Println("\n--- Example 2: Form Fragment Encoding ---")
fmt.Println("Encoded:", sample2)
decoded2, err := decodeQueryString(sample2)
if err != nil {
fmt.Printf("Error decoding: %v\n", err)
} else {
fmt.Println("Decoded:", decoded2)
}
}
/*
run:
--- Example 1: Standard URL Encoding ---
Encoded: Hello%20World%21%20Go%20%26%20Web
Decoded: Hello World! Go & Web
--- Example 2: Form Fragment Encoding ---
Encoded: Jane+Doe+from+New+York
Decoded: Jane Doe from New York
*/