Source file
src/net/url/example_test.go
1
2
3
4
5 package url_test
6
7 import (
8 "encoding/json"
9 "fmt"
10 "log"
11 "net/url"
12 "strings"
13 )
14
15 func ExamplePathEscape() {
16 path := url.PathEscape("my/cool+blog&about,stuff")
17 fmt.Println(path)
18
19
20
21 }
22
23 func ExamplePathUnescape() {
24 escapedPath := "my%2Fcool+blog&about%2Cstuff"
25 path, err := url.PathUnescape(escapedPath)
26 if err != nil {
27 log.Fatal(err)
28 }
29 fmt.Println(path)
30
31
32
33 }
34
35 func ExampleQueryEscape() {
36 query := url.QueryEscape("my/cool+blog&about,stuff")
37 fmt.Println(query)
38
39
40
41 }
42
43 func ExampleQueryUnescape() {
44 escapedQuery := "my%2Fcool%2Bblog%26about%2Cstuff"
45 query, err := url.QueryUnescape(escapedQuery)
46 if err != nil {
47 log.Fatal(err)
48 }
49 fmt.Println(query)
50
51
52
53 }
54
55 func ExampleValues() {
56 v := url.Values{}
57 v.Set("name", "Ava")
58 v.Add("friend", "Jess")
59 v.Add("friend", "Sarah")
60 v.Add("friend", "Zoe")
61
62 fmt.Println(v.Get("name"))
63 fmt.Println(v.Get("friend"))
64 fmt.Println(v["friend"])
65
66
67
68
69 }
70
71 func ExampleValues_Add() {
72 v := url.Values{}
73 v.Add("cat sounds", "meow")
74 v.Add("cat sounds", "mew")
75 v.Add("cat sounds", "mau")
76 fmt.Println(v["cat sounds"])
77
78
79
80 }
81
82 func ExampleValues_Del() {
83 v := url.Values{}
84 v.Add("cat sounds", "meow")
85 v.Add("cat sounds", "mew")
86 v.Add("cat sounds", "mau")
87 fmt.Println(v["cat sounds"])
88
89 v.Del("cat sounds")
90 fmt.Println(v["cat sounds"])
91
92
93
94
95 }
96
97 func ExampleValues_Encode() {
98 v := url.Values{}
99 v.Add("cat sounds", "meow")
100 v.Add("cat sounds", "mew/")
101 v.Add("cat sounds", "mau$")
102 fmt.Println(v.Encode())
103
104
105
106 }
107
108 func ExampleValues_Get() {
109 v := url.Values{}
110 v.Add("cat sounds", "meow")
111 v.Add("cat sounds", "mew")
112 v.Add("cat sounds", "mau")
113 fmt.Printf("%q\n", v.Get("cat sounds"))
114 fmt.Printf("%q\n", v.Get("dog sounds"))
115
116
117
118
119 }
120
121 func ExampleValues_Has() {
122 v := url.Values{}
123 v.Add("cat sounds", "meow")
124 v.Add("cat sounds", "mew")
125 v.Add("cat sounds", "mau")
126 fmt.Println(v.Has("cat sounds"))
127 fmt.Println(v.Has("dog sounds"))
128
129
130
131
132 }
133
134 func ExampleValues_Set() {
135 v := url.Values{}
136 v.Add("cat sounds", "meow")
137 v.Add("cat sounds", "mew")
138 v.Add("cat sounds", "mau")
139 fmt.Println(v["cat sounds"])
140
141 v.Set("cat sounds", "meow")
142 fmt.Println(v["cat sounds"])
143
144
145
146
147 }
148
149 func ExampleURL() {
150 u, err := url.Parse("http://bing.com/search?q=dotnet")
151 if err != nil {
152 log.Fatal(err)
153 }
154 u.Scheme = "https"
155 u.Host = "google.com"
156 q := u.Query()
157 q.Set("q", "golang")
158 u.RawQuery = q.Encode()
159 fmt.Println(u)
160
161 }
162
163 func ExampleURL_roundtrip() {
164
165 u, err := url.Parse("https://example.com/foo%2fbar")
166 if err != nil {
167 log.Fatal(err)
168 }
169 fmt.Println(u.Path)
170 fmt.Println(u.RawPath)
171 fmt.Println(u.String())
172
173
174
175
176 }
177
178 func ExampleURL_ResolveReference() {
179 u, err := url.Parse("../../..//search?q=dotnet")
180 if err != nil {
181 log.Fatal(err)
182 }
183 base, err := url.Parse("http://example.com/directory/")
184 if err != nil {
185 log.Fatal(err)
186 }
187 fmt.Println(base.ResolveReference(u))
188
189
190 }
191
192 func ExampleParseQuery() {
193 m, err := url.ParseQuery(`x=1&y=2&y=3`)
194 if err != nil {
195 log.Fatal(err)
196 }
197 fmt.Println(toJSON(m))
198
199
200 }
201
202 func ExampleURL_EscapedPath() {
203 u, err := url.Parse("http://example.com/x/y%2Fz")
204 if err != nil {
205 log.Fatal(err)
206 }
207 fmt.Println("Path:", u.Path)
208 fmt.Println("RawPath:", u.RawPath)
209 fmt.Println("EscapedPath:", u.EscapedPath())
210
211
212
213
214 }
215
216 func ExampleURL_EscapedFragment() {
217 u, err := url.Parse("http://example.com/#x/y%2Fz")
218 if err != nil {
219 log.Fatal(err)
220 }
221 fmt.Println("Fragment:", u.Fragment)
222 fmt.Println("RawFragment:", u.RawFragment)
223 fmt.Println("EscapedFragment:", u.EscapedFragment())
224
225
226
227
228 }
229
230 func ExampleURL_Hostname() {
231 u, err := url.Parse("https://example.org:8000/path")
232 if err != nil {
233 log.Fatal(err)
234 }
235 fmt.Println(u.Hostname())
236 u, err = url.Parse("https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000")
237 if err != nil {
238 log.Fatal(err)
239 }
240 fmt.Println(u.Hostname())
241
242
243
244 }
245
246 func ExampleURL_IsAbs() {
247 u := url.URL{Host: "example.com", Path: "foo"}
248 fmt.Println(u.IsAbs())
249 u.Scheme = "http"
250 fmt.Println(u.IsAbs())
251
252
253
254 }
255
256 func ExampleURL_JoinPath() {
257 u, err := url.Parse("https://example.com/foo/bar")
258 if err != nil {
259 log.Fatal(err)
260 }
261
262 fmt.Println(u.JoinPath("baz", "qux"))
263
264
265
266 }
267
268 func ExampleURL_MarshalBinary() {
269 u, _ := url.Parse("https://example.org")
270 b, err := u.MarshalBinary()
271 if err != nil {
272 log.Fatal(err)
273 }
274 fmt.Printf("%s\n", b)
275
276
277 }
278
279 func ExampleURL_Parse() {
280 u, err := url.Parse("https://example.org")
281 if err != nil {
282 log.Fatal(err)
283 }
284 rel, err := u.Parse("/foo")
285 if err != nil {
286 log.Fatal(err)
287 }
288 fmt.Println(rel)
289 _, err = u.Parse(":foo")
290 if _, ok := err.(*url.Error); !ok {
291 log.Fatal(err)
292 }
293
294
295 }
296
297 func ExampleURL_Port() {
298 u, err := url.Parse("https://example.org")
299 if err != nil {
300 log.Fatal(err)
301 }
302 fmt.Println(u.Port())
303 u, err = url.Parse("https://example.org:8080")
304 if err != nil {
305 log.Fatal(err)
306 }
307 fmt.Println(u.Port())
308
309
310
311 }
312
313 func ExampleURL_Query() {
314 u, err := url.Parse("https://example.org/?a=1&a=2&b=&=3&&&&")
315 if err != nil {
316 log.Fatal(err)
317 }
318 q := u.Query()
319 fmt.Println(q["a"])
320 fmt.Println(q.Get("b"))
321 fmt.Println(q.Get(""))
322
323
324
325
326 }
327
328 func ExampleURL_String() {
329 u := &url.URL{
330 Scheme: "https",
331 User: url.UserPassword("me", "pass"),
332 Host: "example.com",
333 Path: "foo/bar",
334 RawQuery: "x=1&y=2",
335 Fragment: "anchor",
336 }
337 fmt.Println(u.String())
338 u.Opaque = "opaque"
339 fmt.Println(u.String())
340
341
342
343 }
344
345 func ExampleURL_UnmarshalBinary() {
346 u := &url.URL{}
347 err := u.UnmarshalBinary([]byte("https://example.org/foo"))
348 if err != nil {
349 log.Fatal(err)
350 }
351 fmt.Printf("%s\n", u)
352
353
354 }
355
356 func ExampleURL_Redacted() {
357 u := &url.URL{
358 Scheme: "https",
359 User: url.UserPassword("user", "password"),
360 Host: "example.com",
361 Path: "foo/bar",
362 }
363 fmt.Println(u.Redacted())
364 u.User = url.UserPassword("me", "newerPassword")
365 fmt.Println(u.Redacted())
366
367
368
369 }
370
371 func ExampleURL_RequestURI() {
372 u, err := url.Parse("https://example.org/path?foo=bar")
373 if err != nil {
374 log.Fatal(err)
375 }
376 fmt.Println(u.RequestURI())
377
378 }
379
380 func toJSON(m any) string {
381 js, err := json.Marshal(m)
382 if err != nil {
383 log.Fatal(err)
384 }
385 return strings.ReplaceAll(string(js), ",", ", ")
386 }
387
View as plain text