Source file
src/net/url/example_test.go
Documentation: net/url
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 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
22 fmt.Println(v.Get("name"))
23 fmt.Println(v.Get("friend"))
24 fmt.Println(v["friend"])
25
26
27
28
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
43 }
44
45 func ExampleURL_roundtrip() {
46
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
55
56
57
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
71
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
81
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
93
94
95
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
107
108
109
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
124
125
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
134
135
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
146
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
164
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
179
180
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
193
194
195
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
211
212
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
223
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
237
238
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
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 }
257
View as plain text