url.Parse, URL.String, URL.Query, URL.RequestURI, URL.Redacted
func Parse(rawURL string) (*URL, error)
func (u *URL) String() string
func (u *URL) Query() Values
func (u *URL) RequestURI() string
func (u *URL) Redacted() stringParse a URL into fields, reassemble it, read query values, or hide a password.
| Parameter | Description |
|---|---|
rawURL | The input string. It may be relative or absolute. This helper does not fetch the URL. |
Returns: Parse returns a *URL and an error. String and RequestURI return reassembled strings. Query returns Values derived from RawQuery. Redacted returns a copy with a password replaced by xxxxx.
Errors
error— Parse returns an error for some invalid inputs, but a missing scheme is not always rejected. See the next example.
- User.String() in this example is "user:pass". Prefer Redacted when logging a URL that may contain a password.
- Query silently discards malformed value pairs. ParseQuery reports the first decoding error and still returns the valid parameters.
package main
import (
"fmt"
"net/url"
)
func main() {
u, err := url.Parse("https://user:[email protected]:443/a/b?q=1&q=2#frag")
fmt.Println(err)
fmt.Println(u.Scheme, u.User, u.Host, u.Path, u.RawQuery, u.Fragment)
fmt.Println(u.Query()["q"])
fmt.Println(u.String())
fmt.Println(u.RequestURI())
fmt.Println(u.Redacted())
}Expected output
<nil>
https user:pass example.com:443 /a/b q=1&q=2 frag
[1 2]
https://user:[email protected]:443/a/b?q=1&q=2#frag
/a/b?q=1&q=2
https://user:[email protected]:443/a/b?q=1&q=2#frag