github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/go/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`) 76 if err != nil { 77 log.Fatal(err) 78 } 79 fmt.Println(toJSON(m)) 80 // Output: 81 // {"x":["1"], "y":["2", "3"]} 82 } 83 84 func ExampleURL_EscapedPath() { 85 u, err := url.Parse("http://example.com/x/y%2Fz") 86 if err != nil { 87 log.Fatal(err) 88 } 89 fmt.Println("Path:", u.Path) 90 fmt.Println("RawPath:", u.RawPath) 91 fmt.Println("EscapedPath:", u.EscapedPath()) 92 // Output: 93 // Path: /x/y/z 94 // RawPath: /x/y%2Fz 95 // EscapedPath: /x/y%2Fz 96 } 97 98 func ExampleURL_EscapedFragment() { 99 u, err := url.Parse("http://example.com/#x/y%2Fz") 100 if err != nil { 101 log.Fatal(err) 102 } 103 fmt.Println("Fragment:", u.Fragment) 104 fmt.Println("RawFragment:", u.RawFragment) 105 fmt.Println("EscapedFragment:", u.EscapedFragment()) 106 // Output: 107 // Fragment: x/y/z 108 // RawFragment: x/y%2Fz 109 // EscapedFragment: x/y%2Fz 110 } 111 112 func ExampleURL_Hostname() { 113 u, err := url.Parse("https://example.org:8000/path") 114 if err != nil { 115 log.Fatal(err) 116 } 117 fmt.Println(u.Hostname()) 118 u, err = url.Parse("https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000") 119 if err != nil { 120 log.Fatal(err) 121 } 122 fmt.Println(u.Hostname()) 123 // Output: 124 // example.org 125 // 2001:0db8:85a3:0000:0000:8a2e:0370:7334 126 } 127 128 func ExampleURL_IsAbs() { 129 u := url.URL{Host: "example.com", Path: "foo"} 130 fmt.Println(u.IsAbs()) 131 u.Scheme = "http" 132 fmt.Println(u.IsAbs()) 133 // Output: 134 // false 135 // true 136 } 137 138 func ExampleURL_MarshalBinary() { 139 u, _ := url.Parse("https://example.org") 140 b, err := u.MarshalBinary() 141 if err != nil { 142 log.Fatal(err) 143 } 144 fmt.Printf("%s\n", b) 145 // Output: 146 // https://example.org 147 } 148 149 func ExampleURL_Parse() { 150 u, err := url.Parse("https://example.org") 151 if err != nil { 152 log.Fatal(err) 153 } 154 rel, err := u.Parse("/foo") 155 if err != nil { 156 log.Fatal(err) 157 } 158 fmt.Println(rel) 159 _, err = u.Parse(":foo") 160 if _, ok := err.(*url.Error); !ok { 161 log.Fatal(err) 162 } 163 // Output: 164 // https://example.org/foo 165 } 166 167 func ExampleURL_Port() { 168 u, err := url.Parse("https://example.org") 169 if err != nil { 170 log.Fatal(err) 171 } 172 fmt.Println(u.Port()) 173 u, err = url.Parse("https://example.org:8080") 174 if err != nil { 175 log.Fatal(err) 176 } 177 fmt.Println(u.Port()) 178 // Output: 179 // 180 // 8080 181 } 182 183 func ExampleURL_Query() { 184 u, err := url.Parse("https://example.org/?a=1&a=2&b=&=3&&&&") 185 if err != nil { 186 log.Fatal(err) 187 } 188 q := u.Query() 189 fmt.Println(q["a"]) 190 fmt.Println(q.Get("b")) 191 fmt.Println(q.Get("")) 192 // Output: 193 // [1 2] 194 // 195 // 3 196 } 197 198 func ExampleURL_String() { 199 u := &url.URL{ 200 Scheme: "https", 201 User: url.UserPassword("me", "pass"), 202 Host: "example.com", 203 Path: "foo/bar", 204 RawQuery: "x=1&y=2", 205 Fragment: "anchor", 206 } 207 fmt.Println(u.String()) 208 u.Opaque = "opaque" 209 fmt.Println(u.String()) 210 // Output: 211 // https://me:pass@example.com/foo/bar?x=1&y=2#anchor 212 // https:opaque?x=1&y=2#anchor 213 } 214 215 func ExampleURL_UnmarshalBinary() { 216 u := &url.URL{} 217 err := u.UnmarshalBinary([]byte("https://example.org/foo")) 218 if err != nil { 219 log.Fatal(err) 220 } 221 fmt.Printf("%s\n", u) 222 // Output: 223 // https://example.org/foo 224 } 225 226 func ExampleURL_Redacted() { 227 u := &url.URL{ 228 Scheme: "https", 229 User: url.UserPassword("user", "password"), 230 Host: "example.com", 231 Path: "foo/bar", 232 } 233 fmt.Println(u.Redacted()) 234 u.User = url.UserPassword("me", "newerPassword") 235 fmt.Println(u.Redacted()) 236 // Output: 237 // https://user:xxxxx@example.com/foo/bar 238 // https://me:xxxxx@example.com/foo/bar 239 } 240 241 func ExampleURL_RequestURI() { 242 u, err := url.Parse("https://example.org/path?foo=bar") 243 if err != nil { 244 log.Fatal(err) 245 } 246 fmt.Println(u.RequestURI()) 247 // Output: /path?foo=bar 248 } 249 250 func toJSON(m interface{}) string { 251 js, err := json.Marshal(m) 252 if err != nil { 253 log.Fatal(err) 254 } 255 return strings.ReplaceAll(string(js), ",", ", ") 256 }