1
0
mirror of https://github.com/golang/go synced 2024-11-19 14:34:42 -07:00

net/url: add examples to URL methods

Change-Id: Ia557771272a8ef3602f0281b8c89d2cfe146c8b3
Reviewed-on: https://go-review.googlesource.com/60470
Reviewed-by: Adrian Hesketh <adrianhesketh@hushmail.com>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Guilherme Garnier 2017-08-30 20:06:23 -03:00 committed by Joe Tsai
parent a4e1a72f0a
commit a6a92b1867

View File

@ -81,6 +81,16 @@ func ExampleParseQuery() {
// {"x":["1"], "y":["2", "3"], "z":[""]}
}
func ExampleURL_EscapedPath() {
u, err := url.Parse("http://example.com/path with spaces")
if err != nil {
log.Fatal(err)
}
fmt.Println(u.EscapedPath())
// Output:
// /path%20with%20spaces
}
func ExampleURL_Hostname() {
u, err := url.Parse("https://example.org:8000/path")
if err != nil {
@ -97,6 +107,104 @@ func ExampleURL_Hostname() {
// 2001:0db8:85a3:0000:0000:8a2e:0370:7334
}
func ExampleURL_IsAbs() {
u := url.URL{Host: "example.com", Path: "foo"}
fmt.Println(u.IsAbs())
u.Scheme = "http"
fmt.Println(u.IsAbs())
// Output:
// false
// true
}
func ExampleURL_MarshalBinary() {
u, _ := url.Parse("https://example.org")
b, err := u.MarshalBinary()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", b)
// Output:
// https://example.org
}
func ExampleURL_Parse() {
u, err := url.Parse("https://example.org")
if err != nil {
log.Fatal(err)
}
rel, err := u.Parse("/foo")
if err != nil {
log.Fatal(err)
}
fmt.Println(rel)
_, err = u.Parse(":foo")
if _, ok := err.(*url.Error); !ok {
log.Fatal(err)
}
// Output:
// https://example.org/foo
}
func ExampleURL_Port() {
u, err := url.Parse("https://example.org")
if err != nil {
log.Fatal(err)
}
fmt.Println(u.Port())
u, err = url.Parse("https://example.org:8080")
if err != nil {
log.Fatal(err)
}
fmt.Println(u.Port())
// Output:
//
// 8080
}
func ExampleURL_Query() {
u, err := url.Parse("https://example.org/?a=1&a=2&b=&=3&&&&")
if err != nil {
log.Fatal(err)
}
q := u.Query()
fmt.Println(q["a"])
fmt.Println(q.Get("b"))
fmt.Println(q.Get(""))
// Output:
// [1 2]
//
// 3
}
func ExampleURL_String() {
u := &url.URL{
Scheme: "https",
User: url.UserPassword("me", "pass"),
Host: "example.com",
Path: "foo/bar",
RawQuery: "x=1&y=2",
Fragment: "anchor",
}
fmt.Println(u.String())
u.Opaque = "opaque"
fmt.Println(u.String())
// Output:
// https://me:pass@example.com/foo/bar?x=1&y=2#anchor
// https:opaque?x=1&y=2#anchor
}
func ExampleURL_UnmarshalBinary() {
u := &url.URL{}
err := u.UnmarshalBinary([]byte("https://example.org/foo"))
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", u)
// Output:
// https://example.org/foo
}
func ExampleURL_RequestURI() {
u, err := url.Parse("https://example.org/path?foo=bar")
if err != nil {