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