Commit a6a92b18 authored by Guilherme Garnier's avatar Guilherme Garnier Committed by Joe Tsai

net/url: add examples to URL methods

Change-Id: Ia557771272a8ef3602f0281b8c89d2cfe146c8b3
Reviewed-on: https://go-review.googlesource.com/60470Reviewed-by: default avatarAdrian Hesketh <adrianhesketh@hushmail.com>
Reviewed-by: default avatarJoe Tsai <thebrokentoaster@gmail.com>
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent a4e1a72f
......@@ -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 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment