summaryrefslogtreecommitdiff
path: root/devdocs/go/net%2Furl%2Findex.html
blob: 2c7736762fdd35ac0eb539d3039d1abb96678d35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<h1> Package url  </h1>     <ul id="short-nav">
<li><code>import "net/url"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
</ul>     <h2 id="pkg-overview">Overview </h2> <p>Package url parses URLs and implements query escaping. </p>     <h2 id="pkg-index">Index </h2>  <ul id="manual-nav">
<li><a href="#JoinPath">func JoinPath(base string, elem ...string) (result string, err error)</a></li>
<li><a href="#PathEscape">func PathEscape(s string) string</a></li>
<li><a href="#PathUnescape">func PathUnescape(s string) (string, error)</a></li>
<li><a href="#QueryEscape">func QueryEscape(s string) string</a></li>
<li><a href="#QueryUnescape">func QueryUnescape(s string) (string, error)</a></li>
<li><a href="#Error">type Error</a></li>
<li> <a href="#Error.Error">func (e *Error) Error() string</a>
</li>
<li> <a href="#Error.Temporary">func (e *Error) Temporary() bool</a>
</li>
<li> <a href="#Error.Timeout">func (e *Error) Timeout() bool</a>
</li>
<li> <a href="#Error.Unwrap">func (e *Error) Unwrap() error</a>
</li>
<li><a href="#EscapeError">type EscapeError</a></li>
<li> <a href="#EscapeError.Error">func (e EscapeError) Error() string</a>
</li>
<li><a href="#InvalidHostError">type InvalidHostError</a></li>
<li> <a href="#InvalidHostError.Error">func (e InvalidHostError) Error() string</a>
</li>
<li><a href="#URL">type URL</a></li>
<li> <a href="#Parse">func Parse(rawURL string) (*URL, error)</a>
</li>
<li> <a href="#ParseRequestURI">func ParseRequestURI(rawURL string) (*URL, error)</a>
</li>
<li> <a href="#URL.EscapedFragment">func (u *URL) EscapedFragment() string</a>
</li>
<li> <a href="#URL.EscapedPath">func (u *URL) EscapedPath() string</a>
</li>
<li> <a href="#URL.Hostname">func (u *URL) Hostname() string</a>
</li>
<li> <a href="#URL.IsAbs">func (u *URL) IsAbs() bool</a>
</li>
<li> <a href="#URL.JoinPath">func (u *URL) JoinPath(elem ...string) *URL</a>
</li>
<li> <a href="#URL.MarshalBinary">func (u *URL) MarshalBinary() (text []byte, err error)</a>
</li>
<li> <a href="#URL.Parse">func (u *URL) Parse(ref string) (*URL, error)</a>
</li>
<li> <a href="#URL.Port">func (u *URL) Port() string</a>
</li>
<li> <a href="#URL.Query">func (u *URL) Query() Values</a>
</li>
<li> <a href="#URL.Redacted">func (u *URL) Redacted() string</a>
</li>
<li> <a href="#URL.RequestURI">func (u *URL) RequestURI() string</a>
</li>
<li> <a href="#URL.ResolveReference">func (u *URL) ResolveReference(ref *URL) *URL</a>
</li>
<li> <a href="#URL.String">func (u *URL) String() string</a>
</li>
<li> <a href="#URL.UnmarshalBinary">func (u *URL) UnmarshalBinary(text []byte) error</a>
</li>
<li><a href="#Userinfo">type Userinfo</a></li>
<li> <a href="#User">func User(username string) *Userinfo</a>
</li>
<li> <a href="#UserPassword">func UserPassword(username, password string) *Userinfo</a>
</li>
<li> <a href="#Userinfo.Password">func (u *Userinfo) Password() (string, bool)</a>
</li>
<li> <a href="#Userinfo.String">func (u *Userinfo) String() string</a>
</li>
<li> <a href="#Userinfo.Username">func (u *Userinfo) Username() string</a>
</li>
<li><a href="#Values">type Values</a></li>
<li> <a href="#ParseQuery">func ParseQuery(query string) (Values, error)</a>
</li>
<li> <a href="#Values.Add">func (v Values) Add(key, value string)</a>
</li>
<li> <a href="#Values.Del">func (v Values) Del(key string)</a>
</li>
<li> <a href="#Values.Encode">func (v Values) Encode() string</a>
</li>
<li> <a href="#Values.Get">func (v Values) Get(key string) string</a>
</li>
<li> <a href="#Values.Has">func (v Values) Has(key string) bool</a>
</li>
<li> <a href="#Values.Set">func (v Values) Set(key, value string)</a>
</li>
</ul> <div id="pkg-examples"> <h3>Examples</h3>  <dl> <dd><a class="exampleLink" href="#example_ParseQuery">ParseQuery</a></dd> <dd><a class="exampleLink" href="#example_PathEscape">PathEscape</a></dd> <dd><a class="exampleLink" href="#example_PathUnescape">PathUnescape</a></dd> <dd><a class="exampleLink" href="#example_QueryEscape">QueryEscape</a></dd> <dd><a class="exampleLink" href="#example_QueryUnescape">QueryUnescape</a></dd> <dd><a class="exampleLink" href="#example_URL">URL</a></dd> <dd><a class="exampleLink" href="#example_URL_EscapedFragment">URL.EscapedFragment</a></dd> <dd><a class="exampleLink" href="#example_URL_EscapedPath">URL.EscapedPath</a></dd> <dd><a class="exampleLink" href="#example_URL_Hostname">URL.Hostname</a></dd> <dd><a class="exampleLink" href="#example_URL_IsAbs">URL.IsAbs</a></dd> <dd><a class="exampleLink" href="#example_URL_MarshalBinary">URL.MarshalBinary</a></dd> <dd><a class="exampleLink" href="#example_URL_Parse">URL.Parse</a></dd> <dd><a class="exampleLink" href="#example_URL_Port">URL.Port</a></dd> <dd><a class="exampleLink" href="#example_URL_Query">URL.Query</a></dd> <dd><a class="exampleLink" href="#example_URL_Redacted">URL.Redacted</a></dd> <dd><a class="exampleLink" href="#example_URL_RequestURI">URL.RequestURI</a></dd> <dd><a class="exampleLink" href="#example_URL_ResolveReference">URL.ResolveReference</a></dd> <dd><a class="exampleLink" href="#example_URL_String">URL.String</a></dd> <dd><a class="exampleLink" href="#example_URL_UnmarshalBinary">URL.UnmarshalBinary</a></dd> <dd><a class="exampleLink" href="#example_URL_roundtrip">URL (Roundtrip)</a></dd> <dd><a class="exampleLink" href="#example_Values">Values</a></dd> <dd><a class="exampleLink" href="#example_Values_Add">Values.Add</a></dd> <dd><a class="exampleLink" href="#example_Values_Del">Values.Del</a></dd> <dd><a class="exampleLink" href="#example_Values_Encode">Values.Encode</a></dd> <dd><a class="exampleLink" href="#example_Values_Get">Values.Get</a></dd> <dd><a class="exampleLink" href="#example_Values_Has">Values.Has</a></dd> <dd><a class="exampleLink" href="#example_Values_Set">Values.Set</a></dd> </dl> </div> <h3>Package files</h3> <p>  <span>url.go</span>  </p>   <h2 id="JoinPath">func <span>JoinPath</span>  <span title="Added in Go 1.19">1.19</span> </h2> <pre data-language="go">func JoinPath(base string, elem ...string) (result string, err error)</pre> <p>JoinPath returns a <a href="#URL">URL</a> string with the provided path elements joined to the existing path of base and the resulting path cleaned of any ./ or ../ elements. </p>
<h2 id="PathEscape">func <span>PathEscape</span>  <span title="Added in Go 1.8">1.8</span> </h2> <pre data-language="go">func PathEscape(s string) string</pre> <p>PathEscape escapes the string so it can be safely placed inside a <a href="#URL">URL</a> path segment, replacing special characters (including /) with %XX sequences as needed. </p>   <h4 id="example_PathEscape"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">path := url.PathEscape("my/cool+blog&amp;about,stuff")
fmt.Println(path)

</pre> <p>Output:</p> <pre class="output" data-language="go">my%2Fcool+blog&amp;about%2Cstuff
</pre>   <h2 id="PathUnescape">func <span>PathUnescape</span>  <span title="Added in Go 1.8">1.8</span> </h2> <pre data-language="go">func PathUnescape(s string) (string, error)</pre> <p>PathUnescape does the inverse transformation of <a href="#PathEscape">PathEscape</a>, converting each 3-byte encoded substring of the form "%AB" into the hex-decoded byte 0xAB. It returns an error if any % is not followed by two hexadecimal digits. </p>
<p>PathUnescape is identical to <a href="#QueryUnescape">QueryUnescape</a> except that it does not unescape '+' to ' ' (space). </p>   <h4 id="example_PathUnescape"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">escapedPath := "my%2Fcool+blog&amp;about%2Cstuff"
path, err := url.PathUnescape(escapedPath)
if err != nil {
    log.Fatal(err)
}
fmt.Println(path)

</pre> <p>Output:</p> <pre class="output" data-language="go">my/cool+blog&amp;about,stuff
</pre>   <h2 id="QueryEscape">func <span>QueryEscape</span>  </h2> <pre data-language="go">func QueryEscape(s string) string</pre> <p>QueryEscape escapes the string so it can be safely placed inside a <a href="#URL">URL</a> query. </p>   <h4 id="example_QueryEscape"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">query := url.QueryEscape("my/cool+blog&amp;about,stuff")
fmt.Println(query)

</pre> <p>Output:</p> <pre class="output" data-language="go">my%2Fcool%2Bblog%26about%2Cstuff
</pre>   <h2 id="QueryUnescape">func <span>QueryUnescape</span>  </h2> <pre data-language="go">func QueryUnescape(s string) (string, error)</pre> <p>QueryUnescape does the inverse transformation of <a href="#QueryEscape">QueryEscape</a>, converting each 3-byte encoded substring of the form "%AB" into the hex-decoded byte 0xAB. It returns an error if any % is not followed by two hexadecimal digits. </p>   <h4 id="example_QueryUnescape"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">escapedQuery := "my%2Fcool%2Bblog%26about%2Cstuff"
query, err := url.QueryUnescape(escapedQuery)
if err != nil {
    log.Fatal(err)
}
fmt.Println(query)

</pre> <p>Output:</p> <pre class="output" data-language="go">my/cool+blog&amp;about,stuff
</pre>   <h2 id="Error">type <span>Error</span>  </h2> <p>Error reports an error and the operation and URL that caused it. </p>
<pre data-language="go">type Error struct {
    Op  string
    URL string
    Err error
}
</pre> <h3 id="Error.Error">func (*Error) <span>Error</span>  </h3> <pre data-language="go">func (e *Error) Error() string</pre> <h3 id="Error.Temporary">func (*Error) <span>Temporary</span>  <span title="Added in Go 1.6">1.6</span> </h3> <pre data-language="go">func (e *Error) Temporary() bool</pre> <h3 id="Error.Timeout">func (*Error) <span>Timeout</span>  <span title="Added in Go 1.6">1.6</span> </h3> <pre data-language="go">func (e *Error) Timeout() bool</pre> <h3 id="Error.Unwrap">func (*Error) <span>Unwrap</span>  <span title="Added in Go 1.13">1.13</span> </h3> <pre data-language="go">func (e *Error) Unwrap() error</pre> <h2 id="EscapeError">type <span>EscapeError</span>  </h2> <pre data-language="go">type EscapeError string</pre> <h3 id="EscapeError.Error">func (EscapeError) <span>Error</span>  </h3> <pre data-language="go">func (e EscapeError) Error() string</pre> <h2 id="InvalidHostError">type <span>InvalidHostError</span>  <span title="Added in Go 1.6">1.6</span> </h2> <pre data-language="go">type InvalidHostError string</pre> <h3 id="InvalidHostError.Error">func (InvalidHostError) <span>Error</span>  <span title="Added in Go 1.6">1.6</span> </h3> <pre data-language="go">func (e InvalidHostError) Error() string</pre> <h2 id="URL">type <span>URL</span>  </h2> <p>A URL represents a parsed URL (technically, a URI reference). </p>
<p>The general form represented is: </p>
<pre data-language="go">[scheme:][//[userinfo@]host][/]path[?query][#fragment]
</pre> <p>URLs that do not start with a slash after the scheme are interpreted as: </p>
<pre data-language="go">scheme:opaque[?query][#fragment]
</pre> <p>The Host field contains the host and port subcomponents of the URL. When the port is present, it is separated from the host with a colon. When the host is an IPv6 address, it must be enclosed in square brackets: "[fe80::1]:80". The <span>net.JoinHostPort</span> function combines a host and port into a string suitable for the Host field, adding square brackets to the host when necessary. </p>
<p>Note that the Path field is stored in decoded form: /%47%6f%2f becomes /Go/. A consequence is that it is impossible to tell which slashes in the Path were slashes in the raw URL and which were %2f. This distinction is rarely important, but when it is, the code should use the <a href="#URL.EscapedPath">URL.EscapedPath</a> method, which preserves the original encoding of Path. </p>
<p>The RawPath field is an optional field which is only set when the default encoding of Path is different from the escaped path. See the EscapedPath method for more details. </p>
<p>URL's String method uses the EscapedPath method to obtain the path. </p>
<pre data-language="go">type URL struct {
    Scheme      string
    Opaque      string    // encoded opaque data
    User        *Userinfo // username and password information
    Host        string    // host or host:port (see Hostname and Port methods)
    Path        string    // path (relative paths may omit leading slash)
    RawPath     string    // encoded path hint (see EscapedPath method); added in Go 1.5
    OmitHost    bool      // do not emit empty host (authority); added in Go 1.19
    ForceQuery  bool      // append a query ('?') even if RawQuery is empty; added in Go 1.7
    RawQuery    string    // encoded query values, without '?'
    Fragment    string    // fragment for references, without '#'
    RawFragment string    // encoded fragment hint (see EscapedFragment method); added in Go 1.15
}
</pre>    <h4 id="example_URL"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">u, err := url.Parse("http://bing.com/search?q=dotnet")
if err != nil {
    log.Fatal(err)
}
u.Scheme = "https"
u.Host = "google.com"
q := u.Query()
q.Set("q", "golang")
u.RawQuery = q.Encode()
fmt.Println(u)
</pre> <p>Output:</p> <pre class="output" data-language="go">https://google.com/search?q=golang
</pre>      <h4 id="example_URL_roundtrip"> <span class="text">Example (Roundtrip)</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// Parse + String preserve the original encoding.
u, err := url.Parse("https://example.com/foo%2fbar")
if err != nil {
    log.Fatal(err)
}
fmt.Println(u.Path)
fmt.Println(u.RawPath)
fmt.Println(u.String())
</pre> <p>Output:</p> <pre class="output" data-language="go">/foo/bar
/foo%2fbar
https://example.com/foo%2fbar
</pre>   <h3 id="Parse">func <span>Parse</span>  </h3> <pre data-language="go">func Parse(rawURL string) (*URL, error)</pre> <p>Parse parses a raw url into a <a href="#URL">URL</a> structure. </p>
<p>The url may be relative (a path, without a host) or absolute (starting with a scheme). Trying to parse a hostname and path without a scheme is invalid but may not necessarily return an error, due to parsing ambiguities. </p>
<h3 id="ParseRequestURI">func <span>ParseRequestURI</span>  </h3> <pre data-language="go">func ParseRequestURI(rawURL string) (*URL, error)</pre> <p>ParseRequestURI parses a raw url into a <a href="#URL">URL</a> structure. It assumes that url was received in an HTTP request, so the url is interpreted only as an absolute URI or an absolute path. The string url is assumed not to have a #fragment suffix. (Web browsers strip #fragment before sending the URL to a web server.) </p>
<h3 id="URL.EscapedFragment">func (*URL) <span>EscapedFragment</span>  <span title="Added in Go 1.15">1.15</span> </h3> <pre data-language="go">func (u *URL) EscapedFragment() string</pre> <p>EscapedFragment returns the escaped form of u.Fragment. In general there are multiple possible escaped forms of any fragment. EscapedFragment returns u.RawFragment when it is a valid escaping of u.Fragment. Otherwise EscapedFragment ignores u.RawFragment and computes an escaped form on its own. The <a href="#URL.String">URL.String</a> method uses EscapedFragment to construct its result. In general, code should call EscapedFragment instead of reading u.RawFragment directly. </p>   <h4 id="example_URL_EscapedFragment"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">u, err := url.Parse("http://example.com/#x/y%2Fz")
if err != nil {
    log.Fatal(err)
}
fmt.Println("Fragment:", u.Fragment)
fmt.Println("RawFragment:", u.RawFragment)
fmt.Println("EscapedFragment:", u.EscapedFragment())
</pre> <p>Output:</p> <pre class="output" data-language="go">Fragment: x/y/z
RawFragment: x/y%2Fz
EscapedFragment: x/y%2Fz
</pre>   <h3 id="URL.EscapedPath">func (*URL) <span>EscapedPath</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (u *URL) EscapedPath() string</pre> <p>EscapedPath returns the escaped form of u.Path. In general there are multiple possible escaped forms of any path. EscapedPath returns u.RawPath when it is a valid escaping of u.Path. Otherwise EscapedPath ignores u.RawPath and computes an escaped form on its own. The <a href="#URL.String">URL.String</a> and <a href="#URL.RequestURI">URL.RequestURI</a> methods use EscapedPath to construct their results. In general, code should call EscapedPath instead of reading u.RawPath directly. </p>   <h4 id="example_URL_EscapedPath"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">u, err := url.Parse("http://example.com/x/y%2Fz")
if err != nil {
    log.Fatal(err)
}
fmt.Println("Path:", u.Path)
fmt.Println("RawPath:", u.RawPath)
fmt.Println("EscapedPath:", u.EscapedPath())
</pre> <p>Output:</p> <pre class="output" data-language="go">Path: /x/y/z
RawPath: /x/y%2Fz
EscapedPath: /x/y%2Fz
</pre>   <h3 id="URL.Hostname">func (*URL) <span>Hostname</span>  <span title="Added in Go 1.8">1.8</span> </h3> <pre data-language="go">func (u *URL) Hostname() string</pre> <p>Hostname returns u.Host, stripping any valid port number if present. </p>
<p>If the result is enclosed in square brackets, as literal IPv6 addresses are, the square brackets are removed from the result. </p>   <h4 id="example_URL_Hostname"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">u, err := url.Parse("https://example.org:8000/path")
if err != nil {
    log.Fatal(err)
}
fmt.Println(u.Hostname())
u, err = url.Parse("https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000")
if err != nil {
    log.Fatal(err)
}
fmt.Println(u.Hostname())
</pre> <p>Output:</p> <pre class="output" data-language="go">example.org
2001:0db8:85a3:0000:0000:8a2e:0370:7334
</pre>   <h3 id="URL.IsAbs">func (*URL) <span>IsAbs</span>  </h3> <pre data-language="go">func (u *URL) IsAbs() bool</pre> <p>IsAbs reports whether the <a href="#URL">URL</a> is absolute. Absolute means that it has a non-empty scheme. </p>   <h4 id="example_URL_IsAbs"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">u := url.URL{Host: "example.com", Path: "foo"}
fmt.Println(u.IsAbs())
u.Scheme = "http"
fmt.Println(u.IsAbs())
</pre> <p>Output:</p> <pre class="output" data-language="go">false
true
</pre>   <h3 id="URL.JoinPath">func (*URL) <span>JoinPath</span>  <span title="Added in Go 1.19">1.19</span> </h3> <pre data-language="go">func (u *URL) JoinPath(elem ...string) *URL</pre> <p>JoinPath returns a new <a href="#URL">URL</a> with the provided path elements joined to any existing path and the resulting path cleaned of any ./ or ../ elements. Any sequences of multiple / characters will be reduced to a single /. </p>
<h3 id="URL.MarshalBinary">func (*URL) <span>MarshalBinary</span>  <span title="Added in Go 1.8">1.8</span> </h3> <pre data-language="go">func (u *URL) MarshalBinary() (text []byte, err error)</pre>    <h4 id="example_URL_MarshalBinary"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">u, _ := url.Parse("https://example.org")
b, err := u.MarshalBinary()
if err != nil {
    log.Fatal(err)
}
fmt.Printf("%s\n", b)
</pre> <p>Output:</p> <pre class="output" data-language="go">https://example.org
</pre>   <h3 id="URL.Parse">func (*URL) <span>Parse</span>  </h3> <pre data-language="go">func (u *URL) Parse(ref string) (*URL, error)</pre> <p>Parse parses a <a href="#URL">URL</a> in the context of the receiver. The provided URL may be relative or absolute. Parse returns nil, err on parse failure, otherwise its return value is the same as <a href="#URL.ResolveReference">URL.ResolveReference</a>. </p>   <h4 id="example_URL_Parse"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">u, err := url.Parse("https://example.org")
if err != nil {
    log.Fatal(err)
}
rel, err := u.Parse("/foo")
if err != nil {
    log.Fatal(err)
}
fmt.Println(rel)
_, err = u.Parse(":foo")
if _, ok := err.(*url.Error); !ok {
    log.Fatal(err)
}
</pre> <p>Output:</p> <pre class="output" data-language="go">https://example.org/foo
</pre>   <h3 id="URL.Port">func (*URL) <span>Port</span>  <span title="Added in Go 1.8">1.8</span> </h3> <pre data-language="go">func (u *URL) Port() string</pre> <p>Port returns the port part of u.Host, without the leading colon. </p>
<p>If u.Host doesn't contain a valid numeric port, Port returns an empty string. </p>   <h4 id="example_URL_Port"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">u, err := url.Parse("https://example.org")
if err != nil {
    log.Fatal(err)
}
fmt.Println(u.Port())
u, err = url.Parse("https://example.org:8080")
if err != nil {
    log.Fatal(err)
}
fmt.Println(u.Port())
</pre> <p>Output:</p> <pre class="output" data-language="go">
8080
</pre>   <h3 id="URL.Query">func (*URL) <span>Query</span>  </h3> <pre data-language="go">func (u *URL) Query() Values</pre> <p>Query parses RawQuery and returns the corresponding values. It silently discards malformed value pairs. To check errors use <a href="#ParseQuery">ParseQuery</a>. </p>   <h4 id="example_URL_Query"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">u, err := url.Parse("https://example.org/?a=1&amp;a=2&amp;b=&amp;=3&amp;&amp;&amp;&amp;")
if err != nil {
    log.Fatal(err)
}
q := u.Query()
fmt.Println(q["a"])
fmt.Println(q.Get("b"))
fmt.Println(q.Get(""))
</pre> <p>Output:</p> <pre class="output" data-language="go">[1 2]

3
</pre>   <h3 id="URL.Redacted">func (*URL) <span>Redacted</span>  <span title="Added in Go 1.15">1.15</span> </h3> <pre data-language="go">func (u *URL) Redacted() string</pre> <p>Redacted is like <a href="#URL.String">URL.String</a> but replaces any password with "xxxxx". Only the password in u.User is redacted. </p>   <h4 id="example_URL_Redacted"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">u := &amp;url.URL{
    Scheme: "https",
    User:   url.UserPassword("user", "password"),
    Host:   "example.com",
    Path:   "foo/bar",
}
fmt.Println(u.Redacted())
u.User = url.UserPassword("me", "newerPassword")
fmt.Println(u.Redacted())
</pre> <p>Output:</p> <pre class="output" data-language="go">https://user:xxxxx@example.com/foo/bar
https://me:xxxxx@example.com/foo/bar
</pre>   <h3 id="URL.RequestURI">func (*URL) <span>RequestURI</span>  </h3> <pre data-language="go">func (u *URL) RequestURI() string</pre> <p>RequestURI returns the encoded path?query or opaque?query string that would be used in an HTTP request for u. </p>   <h4 id="example_URL_RequestURI"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">u, err := url.Parse("https://example.org/path?foo=bar")
if err != nil {
    log.Fatal(err)
}
fmt.Println(u.RequestURI())
</pre> <p>Output:</p> <pre class="output" data-language="go">/path?foo=bar
</pre>   <h3 id="URL.ResolveReference">func (*URL) <span>ResolveReference</span>  </h3> <pre data-language="go">func (u *URL) ResolveReference(ref *URL) *URL</pre> <p>ResolveReference resolves a URI reference to an absolute URI from an absolute base URI u, per RFC 3986 Section 5.2. The URI reference may be relative or absolute. ResolveReference always returns a new <a href="#URL">URL</a> instance, even if the returned URL is identical to either the base or reference. If ref is an absolute URL, then ResolveReference ignores base and returns a copy of ref. </p>   <h4 id="example_URL_ResolveReference"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">u, err := url.Parse("../../..//search?q=dotnet")
if err != nil {
    log.Fatal(err)
}
base, err := url.Parse("http://example.com/directory/")
if err != nil {
    log.Fatal(err)
}
fmt.Println(base.ResolveReference(u))
</pre> <p>Output:</p> <pre class="output" data-language="go">http://example.com/search?q=dotnet
</pre>   <h3 id="URL.String">func (*URL) <span>String</span>  </h3> <pre data-language="go">func (u *URL) String() string</pre> <p>String reassembles the <a href="#URL">URL</a> into a valid URL string. The general form of the result is one of: </p>
<pre data-language="go">scheme:opaque?query#fragment
scheme://userinfo@host/path?query#fragment
</pre> <p>If u.Opaque is non-empty, String uses the first form; otherwise it uses the second form. Any non-ASCII characters in host are escaped. To obtain the path, String uses u.EscapedPath(). </p>
<p>In the second form, the following rules apply: </p>
<ul> <li>if u.Scheme is empty, scheme: is omitted. </li>
<li>if u.User is nil, userinfo@ is omitted. </li>
<li>if u.Host is empty, host/ is omitted. </li>
<li>if u.Scheme and u.Host are empty and u.User is nil, the entire scheme://userinfo@host/ is omitted. </li>
<li>if u.Host is non-empty and u.Path begins with a /, the form host/path does not add its own /. </li>
<li>if u.RawQuery is empty, ?query is omitted. </li>
<li>if u.Fragment is empty, #fragment is omitted. </li>
</ul>    <h4 id="example_URL_String"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">u := &amp;url.URL{
    Scheme:   "https",
    User:     url.UserPassword("me", "pass"),
    Host:     "example.com",
    Path:     "foo/bar",
    RawQuery: "x=1&amp;y=2",
    Fragment: "anchor",
}
fmt.Println(u.String())
u.Opaque = "opaque"
fmt.Println(u.String())
</pre> <p>Output:</p> <pre class="output" data-language="go">https://me:pass@example.com/foo/bar?x=1&amp;y=2#anchor
https:opaque?x=1&amp;y=2#anchor
</pre>   <h3 id="URL.UnmarshalBinary">func (*URL) <span>UnmarshalBinary</span>  <span title="Added in Go 1.8">1.8</span> </h3> <pre data-language="go">func (u *URL) UnmarshalBinary(text []byte) error</pre>    <h4 id="example_URL_UnmarshalBinary"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">u := &amp;url.URL{}
err := u.UnmarshalBinary([]byte("https://example.org/foo"))
if err != nil {
    log.Fatal(err)
}
fmt.Printf("%s\n", u)
</pre> <p>Output:</p> <pre class="output" data-language="go">https://example.org/foo
</pre>   <h2 id="Userinfo">type <span>Userinfo</span>  </h2> <p>The Userinfo type is an immutable encapsulation of username and password details for a <a href="#URL">URL</a>. An existing Userinfo value is guaranteed to have a username set (potentially empty, as allowed by RFC 2396), and optionally a password. </p>
<pre data-language="go">type Userinfo struct {
    // contains filtered or unexported fields
}
</pre> <h3 id="User">func <span>User</span>  </h3> <pre data-language="go">func User(username string) *Userinfo</pre> <p>User returns a <a href="#Userinfo">Userinfo</a> containing the provided username and no password set. </p>
<h3 id="UserPassword">func <span>UserPassword</span>  </h3> <pre data-language="go">func UserPassword(username, password string) *Userinfo</pre> <p>UserPassword returns a <a href="#Userinfo">Userinfo</a> containing the provided username and password. </p>
<p>This functionality should only be used with legacy web sites. RFC 2396 warns that interpreting Userinfo this way “is NOT RECOMMENDED, because the passing of authentication information in clear text (such as URI) has proven to be a security risk in almost every case where it has been used.” </p>
<h3 id="Userinfo.Password">func (*Userinfo) <span>Password</span>  </h3> <pre data-language="go">func (u *Userinfo) Password() (string, bool)</pre> <p>Password returns the password in case it is set, and whether it is set. </p>
<h3 id="Userinfo.String">func (*Userinfo) <span>String</span>  </h3> <pre data-language="go">func (u *Userinfo) String() string</pre> <p>String returns the encoded userinfo information in the standard form of "username[:password]". </p>
<h3 id="Userinfo.Username">func (*Userinfo) <span>Username</span>  </h3> <pre data-language="go">func (u *Userinfo) Username() string</pre> <p>Username returns the username. </p>
<h2 id="Values">type <span>Values</span>  </h2> <p>Values maps a string key to a list of values. It is typically used for query parameters and form values. Unlike in the http.Header map, the keys in a Values map are case-sensitive. </p>
<pre data-language="go">type Values map[string][]string</pre>    <h4 id="example_Values"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">v := url.Values{}
v.Set("name", "Ava")
v.Add("friend", "Jess")
v.Add("friend", "Sarah")
v.Add("friend", "Zoe")
// v.Encode() == "name=Ava&amp;friend=Jess&amp;friend=Sarah&amp;friend=Zoe"
fmt.Println(v.Get("name"))
fmt.Println(v.Get("friend"))
fmt.Println(v["friend"])
</pre> <p>Output:</p> <pre class="output" data-language="go">Ava
Jess
[Jess Sarah Zoe]
</pre>   <h3 id="ParseQuery">func <span>ParseQuery</span>  </h3> <pre data-language="go">func ParseQuery(query string) (Values, error)</pre> <p>ParseQuery parses the URL-encoded query string and returns a map listing the values specified for each key. ParseQuery always returns a non-nil map containing all the valid query parameters found; err describes the first decoding error encountered, if any. </p>
<p>Query is expected to be a list of key=value settings separated by ampersands. A setting without an equals sign is interpreted as a key set to an empty value. Settings containing a non-URL-encoded semicolon are considered invalid. </p>   <h4 id="example_ParseQuery"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">m, err := url.ParseQuery(`x=1&amp;y=2&amp;y=3`)
if err != nil {
    log.Fatal(err)
}
fmt.Println(toJSON(m))
</pre> <p>Output:</p> <pre class="output" data-language="go">{"x":["1"], "y":["2", "3"]}
</pre>   <h3 id="Values.Add">func (Values) <span>Add</span>  </h3> <pre data-language="go">func (v Values) Add(key, value string)</pre> <p>Add adds the value to key. It appends to any existing values associated with key. </p>   <h4 id="example_Values_Add"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">v := url.Values{}
v.Add("cat sounds", "meow")
v.Add("cat sounds", "mew")
v.Add("cat sounds", "mau")
fmt.Println(v["cat sounds"])

</pre> <p>Output:</p> <pre class="output" data-language="go">[meow mew mau]
</pre>   <h3 id="Values.Del">func (Values) <span>Del</span>  </h3> <pre data-language="go">func (v Values) Del(key string)</pre> <p>Del deletes the values associated with key. </p>   <h4 id="example_Values_Del"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">v := url.Values{}
v.Add("cat sounds", "meow")
v.Add("cat sounds", "mew")
v.Add("cat sounds", "mau")
fmt.Println(v["cat sounds"])

v.Del("cat sounds")
fmt.Println(v["cat sounds"])

</pre> <p>Output:</p> <pre class="output" data-language="go">[meow mew mau]
[]
</pre>   <h3 id="Values.Encode">func (Values) <span>Encode</span>  </h3> <pre data-language="go">func (v Values) Encode() string</pre> <p>Encode encodes the values into “URL encoded” form ("bar=baz&amp;foo=quux") sorted by key. </p>   <h4 id="example_Values_Encode"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">v := url.Values{}
v.Add("cat sounds", "meow")
v.Add("cat sounds", "mew/")
v.Add("cat sounds", "mau$")
fmt.Println(v.Encode())

</pre> <p>Output:</p> <pre class="output" data-language="go">cat+sounds=meow&amp;cat+sounds=mew%2F&amp;cat+sounds=mau%24
</pre>   <h3 id="Values.Get">func (Values) <span>Get</span>  </h3> <pre data-language="go">func (v Values) Get(key string) string</pre> <p>Get gets the first value associated with the given key. If there are no values associated with the key, Get returns the empty string. To access multiple values, use the map directly. </p>   <h4 id="example_Values_Get"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">v := url.Values{}
v.Add("cat sounds", "meow")
v.Add("cat sounds", "mew")
v.Add("cat sounds", "mau")
fmt.Printf("%q\n", v.Get("cat sounds"))
fmt.Printf("%q\n", v.Get("dog sounds"))

</pre> <p>Output:</p> <pre class="output" data-language="go">"meow"
""
</pre>   <h3 id="Values.Has">func (Values) <span>Has</span>  <span title="Added in Go 1.17">1.17</span> </h3> <pre data-language="go">func (v Values) Has(key string) bool</pre> <p>Has checks whether a given key is set. </p>   <h4 id="example_Values_Has"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">v := url.Values{}
v.Add("cat sounds", "meow")
v.Add("cat sounds", "mew")
v.Add("cat sounds", "mau")
fmt.Println(v.Has("cat sounds"))
fmt.Println(v.Has("dog sounds"))

</pre> <p>Output:</p> <pre class="output" data-language="go">true
false
</pre>   <h3 id="Values.Set">func (Values) <span>Set</span>  </h3> <pre data-language="go">func (v Values) Set(key, value string)</pre> <p>Set sets the key to value. It replaces any existing values. </p>   <h4 id="example_Values_Set"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">v := url.Values{}
v.Add("cat sounds", "meow")
v.Add("cat sounds", "mew")
v.Add("cat sounds", "mau")
fmt.Println(v["cat sounds"])

v.Set("cat sounds", "meow")
fmt.Println(v["cat sounds"])

</pre> <p>Output:</p> <pre class="output" data-language="go">[meow mew mau]
[meow]
</pre><div class="_attribution">
  <p class="_attribution-p">
    &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
    <a href="http://golang.org/pkg/net/url/" class="_attribution-link">http://golang.org/pkg/net/url/</a>
  </p>
</div>