package main
import (
"encoding/json"
"fmt"
"net/url"
)
func main() {
u := &url.URL{}
u.Scheme = "http"
u.Host = "localhost:80"
u.Path = "index.html"
u.RawQuery = "id=1&name=John"
u.User = url.UserPassword("admin", "1234")
fmt.Printf("Assembled URL:
%v
", u)
parsedURL, err := url.Parse(u.String())
if err != nil {
panic(err)
}
jsonURL, err := json.Marshal(parsedURL)
if err != nil {
panic(err)
}
fmt.Println("Parsed URL:")
fmt.Println(string(jsonURL))
}
/*
Assembled URL:
http://admin:1234@localhost:80/index.html?id=1&name=John
Parsed URL:
{"Scheme":"http","Opaque":"","User":{},"Host":"localhost:80","Path":"/index.html","RawPath":"","ForceQuery":false,"RawQuery":"id=1u0026name=John","Fragment":""}
*/