diff --git a/src/net/url/example_test.go b/src/net/url/example_test.go index d352c55b6fa14..d8eb6dcd20c7b 100644 --- a/src/net/url/example_test.go +++ b/src/net/url/example_test.go @@ -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 {