github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/src/net/url/example_test.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package url_test 6 7 import ( 8 "encoding/json" 9 "fmt" 10 "log" 11 "net/url" 12 "strings" 13 ) 14 15 func ExampleValues() { 16 v := url.Values{} 17 v.Set("name", "Ava") 18 v.Add("friend", "Jess") 19 v.Add("friend", "Sarah") 20 v.Add("friend", "Zoe") 21 // v.Encode() == "name=Ava&friend=Jess&friend=Sarah&friend=Zoe" 22 fmt.Println(v.Get("name")) 23 fmt.Println(v.Get("friend")) 24 fmt.Println(v["friend"]) 25 // Output: 26 // Ava 27 // Jess 28 // [Jess Sarah Zoe] 29 } 30 31 func ExampleURL() { 32 u, err := url.Parse("http://bing.com/search?q=dotnet") 33 if err != nil { 34 log.Fatal(err) 35 } 36 u.Scheme = "https" 37 u.Host = "google.com" 38 q := u.Query() 39 q.Set("q", "golang") 40 u.RawQuery = q.Encode() 41 fmt.Println(u) 42 // Output: https://google.com/search?q=golang 43 } 44 45 func ExampleURL_roundtrip() { 46 // Parse + String preserve the original encoding. 47 u, err := url.Parse("https://example.com/foo%2fbar") 48 if err != nil { 49 log.Fatal(err) 50 } 51 fmt.Println(u.Path) 52 fmt.Println(u.RawPath) 53 fmt.Println(u.String()) 54 // Output: 55 // /foo/bar 56 // /foo%2fbar 57 // https://example.com/foo%2fbar 58 } 59 60 func ExampleURL_ResolveReference() { 61 u, err := url.Parse("../../..//search?q=dotnet") 62 if err != nil { 63 log.Fatal(err) 64 } 65 base, err := url.Parse("http://example.com/directory/") 66 if err != nil { 67 log.Fatal(err) 68 } 69 fmt.Println(base.ResolveReference(u)) 70 // Output: 71 // http://example.com/search?q=dotnet 72 } 73 74 func ExampleParseQuery() { 75 m, err := url.ParseQuery(`x=1&y=2&y=3;z`) 76 if err != nil { 77 log.Fatal(err) 78 } 79 fmt.Println(toJSON(m)) 80 // Output: 81 // {"x":["1"], "y":["2", "3"], "z":[""]} 82 } 83 84 func ExampleURL_EscapedPath() { 85 u, err := url.Parse("http://example.com/path with spaces") 86 if err != nil { 87 log.Fatal(err) 88 } 89 fmt.Println(u.EscapedPath()) 90 // Output: 91 // /path%20with%20spaces 92 } 93 94 func ExampleURL_Hostname() { 95 u, err := url.Parse("https://example.org:8000/path") 96 if err != nil { 97 log.Fatal(err) 98 } 99 fmt.Println(u.Hostname()) 100 u, err = url.Parse("https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000") 101 if err != nil { 102 log.Fatal(err) 103 } 104 fmt.Println(u.Hostname()) 105 // Output: 106 // example.org 107 // 2001:0db8:85a3:0000:0000:8a2e:0370:7334 108 } 109 110 func ExampleURL_IsAbs() { 111 u := url.URL{Host: "example.com", Path: "foo"} 112 fmt.Println(u.IsAbs()) 113 u.Scheme = "http" 114 fmt.Println(u.IsAbs()) 115 // Output: 116 // false 117 // true 118 } 119 120 func ExampleURL_MarshalBinary() { 121 u, _ := url.Parse("https://example.org") 122 b, err := u.MarshalBinary() 123 if err != nil { 124 log.Fatal(err) 125 } 126 fmt.Printf("%s\n", b) 127 // Output: 128 // https://example.org 129 } 130 131 func ExampleURL_Parse() { 132 u, err := url.Parse("https://example.org") 133 if err != nil { 134 log.Fatal(err) 135 } 136 rel, err := u.Parse("/foo") 137 if err != nil { 138 log.Fatal(err) 139 } 140 fmt.Println(rel) 141 _, err = u.Parse(":foo") 142 if _, ok := err.(*url.Error); !ok { 143 log.Fatal(err) 144 } 145 // Output: 146 // https://example.org/foo 147 } 148 149 func ExampleURL_Port() { 150 u, err := url.Parse("https://example.org") 151 if err != nil { 152 log.Fatal(err) 153 } 154 fmt.Println(u.Port()) 155 u, err = url.Parse("https://example.org:8080") 156 if err != nil { 157 log.Fatal(err) 158 } 159 fmt.Println(u.Port()) 160 // Output: 161 // 162 // 8080 163 } 164 165 func ExampleURL_Query() { 166 u, err := url.Parse("https://example.org/?a=1&a=2&b=&=3&&&&") 167 if err != nil { 168 log.Fatal(err) 169 } 170 q := u.Query() 171 fmt.Println(q["a"]) 172 fmt.Println(q.Get("b")) 173 fmt.Println(q.Get("")) 174 // Output: 175 // [1 2] 176 // 177 // 3 178 } 179 180 func ExampleURL_String() { 181 u := &url.URL{ 182 Scheme: "https", 183 User: url.UserPassword("me", "pass"), 184 Host: "example.com", 185 Path: "foo/bar", 186 RawQuery: "x=1&y=2", 187 Fragment: "anchor", 188 } 189 fmt.Println(u.String()) 190 u.Opaque = "opaque" 191 fmt.Println(u.String()) 192 // Output: 193 // https://me:pass@example.com/foo/bar?x=1&y=2#anchor 194 // https:opaque?x=1&y=2#anchor 195 } 196 197 func ExampleURL_UnmarshalBinary() { 198 u := &url.URL{} 199 err := u.UnmarshalBinary([]byte("https://example.org/foo")) 200 if err != nil { 201 log.Fatal(err) 202 } 203 fmt.Printf("%s\n", u) 204 // Output: 205 // https://example.org/foo 206 } 207 208 func ExampleURL_RequestURI() { 209 u, err := url.Parse("https://example.org/path?foo=bar") 210 if err != nil { 211 log.Fatal(err) 212 } 213 fmt.Println(u.RequestURI()) 214 // Output: /path?foo=bar 215 } 216 217 func toJSON(m interface{}) string { 218 js, err := json.Marshal(m) 219 if err != nil { 220 log.Fatal(err) 221 } 222 return strings.Replace(string(js), ",", ", ", -1) 223 }