import ( "fmt" "net/url" ) func main() { u := &url.URL{Scheme: "https", Host: "www.example.com", Path: "/search?q=go"} fmt.Println(u.EscapedPath()) // Output: /search%3Fq%3Dgo }
import ( "fmt" "net/url" ) func main() { u := &url.URL{Scheme: "ftp", Host: "ftp.example.com", Path: "/path with spaces/file.txt"} fmt.Println(u.EscapedPath()) // Output: /path%20with%20spaces/file.txt }In this example, we create a URL value with a path that contains spaces. We use the EscapedPath() method to obtain the path component of the URL, properly escaped. The output shows that the spaces have been replaced with %20. Package library: net/url.