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_MarshalBinary() {
257 u, _ := url.Parse("https://example.org")
258 b, err := u.MarshalBinary()
259 if err != nil {
260 log.Fatal(err)
261 }
262 fmt.Printf("%s\n", b)
263
264
265 }
266
267 func ExampleURL_Parse() {
268 u, err := url.Parse("https://example.org")
269 if err != nil {
270 log.Fatal(err)
271 }
272 rel, err := u.Parse("/foo")
273 if err != nil {
274 log.Fatal(err)
275 }
276 fmt.Println(rel)
277 _, err = u.Parse(":foo")
278 if _, ok := err.(*url.Error); !ok {
279 log.Fatal(err)
280 }
281
282
283 }
284
285 func ExampleURL_Port() {
286 u, err := url.Parse("https://example.org")
287 if err != nil {
288 log.Fatal(err)
289 }
290 fmt.Println(u.Port())
291 u, err = url.Parse("https://example.org:8080")
292 if err != nil {
293 log.Fatal(err)
294 }
295 fmt.Println(u.Port())
296
297
298
299 }
300
301 func ExampleURL_Query() {
302 u, err := url.Parse("https://example.org/?a=1&a=2&b=&=3&&&&")
303 if err != nil {
304 log.Fatal(err)
305 }
306 q := u.Query()
307 fmt.Println(q["a"])
308 fmt.Println(q.Get("b"))
309 fmt.Println(q.Get(""))
310
311
312
313
314 }
315
316 func ExampleURL_String() {
317 u := &url.URL{
318 Scheme: "https",
319 User: url.UserPassword("me", "pass"),
320 Host: "example.com",
321 Path: "foo/bar",
322 RawQuery: "x=1&y=2",
323 Fragment: "anchor",
324 }
325 fmt.Println(u.String())
326 u.Opaque = "opaque"
327 fmt.Println(u.String())
328
329
330
331 }
332
333 func ExampleURL_UnmarshalBinary() {
334 u := &url.URL{}
335 err := u.UnmarshalBinary([]byte("https://example.org/foo"))
336 if err != nil {
337 log.Fatal(err)
338 }
339 fmt.Printf("%s\n", u)
340
341
342 }
343
344 func ExampleURL_Redacted() {
345 u := &url.URL{
346 Scheme: "https",
347 User: url.UserPassword("user", "password"),
348 Host: "example.com",
349 Path: "foo/bar",
350 }
351 fmt.Println(u.Redacted())
352 u.User = url.UserPassword("me", "newerPassword")
353 fmt.Println(u.Redacted())
354
355
356
357 }
358
359 func ExampleURL_RequestURI() {
360 u, err := url.Parse("https://example.org/path?foo=bar")
361 if err != nil {
362 log.Fatal(err)
363 }
364 fmt.Println(u.RequestURI())
365
366 }
367
368 func toJSON(m any) string {
369 js, err := json.Marshal(m)
370 if err != nil {
371 log.Fatal(err)
372 }
373 return strings.ReplaceAll(string(js), ",", ", ")
374 }
375
View as plain text