Skip to content

Commit

Permalink
net/url: add examples to URL methods
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ggarnier authored and dsnet committed Sep 1, 2017
1 parent a4e1a72 commit a6a92b1
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions src/net/url/example_test.go
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit a6a92b1

Please sign in to comment.