You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1002 lines
30 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>url - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package url</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "net/url"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package url parses URLs and implements query escaping.
  69. See RFC 3986.
  70. </p>
  71. </div>
  72. </div>
  73. <div id="pkg-index" class="toggleVisible">
  74. <div class="collapsed">
  75. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  76. </div>
  77. <div class="expanded">
  78. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  79. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  80. <div id="manual-nav">
  81. <dl>
  82. <dd><a href="index.html#QueryEscape">func QueryEscape(s string) string</a></dd>
  83. <dd><a href="index.html#QueryUnescape">func QueryUnescape(s string) (string, error)</a></dd>
  84. <dd><a href="index.html#Error">type Error</a></dd>
  85. <dd>&nbsp; &nbsp; <a href="index.html#Error.Error">func (e *Error) Error() string</a></dd>
  86. <dd>&nbsp; &nbsp; <a href="index.html#Error.Temporary">func (e *Error) Temporary() bool</a></dd>
  87. <dd>&nbsp; &nbsp; <a href="index.html#Error.Timeout">func (e *Error) Timeout() bool</a></dd>
  88. <dd><a href="index.html#EscapeError">type EscapeError</a></dd>
  89. <dd>&nbsp; &nbsp; <a href="index.html#EscapeError.Error">func (e EscapeError) Error() string</a></dd>
  90. <dd><a href="index.html#InvalidHostError">type InvalidHostError</a></dd>
  91. <dd>&nbsp; &nbsp; <a href="index.html#InvalidHostError.Error">func (e InvalidHostError) Error() string</a></dd>
  92. <dd><a href="index.html#URL">type URL</a></dd>
  93. <dd>&nbsp; &nbsp; <a href="index.html#Parse">func Parse(rawurl string) (url *URL, err error)</a></dd>
  94. <dd>&nbsp; &nbsp; <a href="index.html#ParseRequestURI">func ParseRequestURI(rawurl string) (url *URL, err error)</a></dd>
  95. <dd>&nbsp; &nbsp; <a href="index.html#URL.EscapedPath">func (u *URL) EscapedPath() string</a></dd>
  96. <dd>&nbsp; &nbsp; <a href="index.html#URL.IsAbs">func (u *URL) IsAbs() bool</a></dd>
  97. <dd>&nbsp; &nbsp; <a href="index.html#URL.Parse">func (u *URL) Parse(ref string) (*URL, error)</a></dd>
  98. <dd>&nbsp; &nbsp; <a href="index.html#URL.Query">func (u *URL) Query() Values</a></dd>
  99. <dd>&nbsp; &nbsp; <a href="index.html#URL.RequestURI">func (u *URL) RequestURI() string</a></dd>
  100. <dd>&nbsp; &nbsp; <a href="index.html#URL.ResolveReference">func (u *URL) ResolveReference(ref *URL) *URL</a></dd>
  101. <dd>&nbsp; &nbsp; <a href="index.html#URL.String">func (u *URL) String() string</a></dd>
  102. <dd><a href="index.html#Userinfo">type Userinfo</a></dd>
  103. <dd>&nbsp; &nbsp; <a href="index.html#User">func User(username string) *Userinfo</a></dd>
  104. <dd>&nbsp; &nbsp; <a href="index.html#UserPassword">func UserPassword(username, password string) *Userinfo</a></dd>
  105. <dd>&nbsp; &nbsp; <a href="index.html#Userinfo.Password">func (u *Userinfo) Password() (string, bool)</a></dd>
  106. <dd>&nbsp; &nbsp; <a href="index.html#Userinfo.String">func (u *Userinfo) String() string</a></dd>
  107. <dd>&nbsp; &nbsp; <a href="index.html#Userinfo.Username">func (u *Userinfo) Username() string</a></dd>
  108. <dd><a href="index.html#Values">type Values</a></dd>
  109. <dd>&nbsp; &nbsp; <a href="index.html#ParseQuery">func ParseQuery(query string) (m Values, err error)</a></dd>
  110. <dd>&nbsp; &nbsp; <a href="index.html#Values.Add">func (v Values) Add(key, value string)</a></dd>
  111. <dd>&nbsp; &nbsp; <a href="index.html#Values.Del">func (v Values) Del(key string)</a></dd>
  112. <dd>&nbsp; &nbsp; <a href="index.html#Values.Encode">func (v Values) Encode() string</a></dd>
  113. <dd>&nbsp; &nbsp; <a href="index.html#Values.Get">func (v Values) Get(key string) string</a></dd>
  114. <dd>&nbsp; &nbsp; <a href="index.html#Values.Set">func (v Values) Set(key, value string)</a></dd>
  115. </dl>
  116. </div><!-- #manual-nav -->
  117. <div id="pkg-examples">
  118. <h4>Examples</h4>
  119. <dl>
  120. <dd><a class="exampleLink" href="index.html#example_URL">URL</a></dd>
  121. <dd><a class="exampleLink" href="index.html#example_URL_ResolveReference">URL.ResolveReference</a></dd>
  122. <dd><a class="exampleLink" href="index.html#example_URL_opaque">URL (Opaque)</a></dd>
  123. <dd><a class="exampleLink" href="index.html#example_URL_roundtrip">URL (Roundtrip)</a></dd>
  124. <dd><a class="exampleLink" href="index.html#example_Values">Values</a></dd>
  125. </dl>
  126. </div>
  127. <h4>Package files</h4>
  128. <p>
  129. <span style="font-size:90%">
  130. <a href="http://localhost:6060/src/net/url/url.go">url.go</a>
  131. </span>
  132. </p>
  133. </div><!-- .expanded -->
  134. </div><!-- #pkg-index -->
  135. <div id="pkg-callgraph" class="toggle" style="display: none">
  136. <div class="collapsed">
  137. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  138. </div> <!-- .expanded -->
  139. <div class="expanded">
  140. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  141. <p>
  142. In the call graph viewer below, each node
  143. is a function belonging to this package
  144. and its children are the functions it
  145. calls&mdash;perhaps dynamically.
  146. </p>
  147. <p>
  148. The root nodes are the entry points of the
  149. package: functions that may be called from
  150. outside the package.
  151. There may be non-exported or anonymous
  152. functions among them if they are called
  153. dynamically from another package.
  154. </p>
  155. <p>
  156. Click a node to visit that function's source code.
  157. From there you can visit its callers by
  158. clicking its declaring <code>func</code>
  159. token.
  160. </p>
  161. <p>
  162. Functions may be omitted if they were
  163. determined to be unreachable in the
  164. particular programs or tests that were
  165. analyzed.
  166. </p>
  167. <!-- Zero means show all package entry points. -->
  168. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  169. </div>
  170. </div> <!-- #pkg-callgraph -->
  171. <h2 id="QueryEscape">func <a href="http://localhost:6060/src/net/url/url.go?s=6331:6364#L235">QueryEscape</a></h2>
  172. <pre>func QueryEscape(s <a href="../../builtin/index.html#string">string</a>) <a href="../../builtin/index.html#string">string</a></pre>
  173. <p>
  174. QueryEscape escapes the string so it can be safely placed
  175. inside a URL query.
  176. </p>
  177. <h2 id="QueryUnescape">func <a href="http://localhost:6060/src/net/url/url.go?s=4059:4103#L149">QueryUnescape</a></h2>
  178. <pre>func QueryUnescape(s <a href="../../builtin/index.html#string">string</a>) (<a href="../../builtin/index.html#string">string</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  179. <p>
  180. QueryUnescape does the inverse transformation of QueryEscape, converting
  181. %AB into the byte 0xAB and &#39;+&#39; into &#39; &#39; (space). It returns an error if
  182. any % is not followed by two hexadecimal digits.
  183. </p>
  184. <h2 id="Error">type <a href="http://localhost:6060/src/net/url/url.go?s=384:440#L9">Error</a></h2>
  185. <pre>type Error struct {
  186. Op <a href="../../builtin/index.html#string">string</a>
  187. URL <a href="../../builtin/index.html#string">string</a>
  188. Err <a href="../../builtin/index.html#error">error</a>
  189. }</pre>
  190. <p>
  191. Error reports an error and the operation and URL that caused it.
  192. </p>
  193. <h3 id="Error.Error">func (*Error) <a href="http://localhost:6060/src/net/url/url.go?s=442:472#L15">Error</a></h3>
  194. <pre>func (e *<a href="index.html#Error">Error</a>) Error() <a href="../../builtin/index.html#string">string</a></pre>
  195. <h3 id="Error.Temporary">func (*Error) <a href="http://localhost:6060/src/net/url/url.go?s=707:739#L30">Temporary</a></h3>
  196. <pre>func (e *<a href="index.html#Error">Error</a>) Temporary() <a href="../../builtin/index.html#bool">bool</a></pre>
  197. <h3 id="Error.Timeout">func (*Error) <a href="http://localhost:6060/src/net/url/url.go?s=571:601#L21">Timeout</a></h3>
  198. <pre>func (e *<a href="index.html#Error">Error</a>) Timeout() <a href="../../builtin/index.html#bool">bool</a></pre>
  199. <h2 id="EscapeError">type <a href="http://localhost:6060/src/net/url/url.go?s=1324:1347#L70">EscapeError</a></h2>
  200. <pre>type EscapeError <a href="../../builtin/index.html#string">string</a></pre>
  201. <h3 id="EscapeError.Error">func (EscapeError) <a href="http://localhost:6060/src/net/url/url.go?s=1349:1384#L72">Error</a></h3>
  202. <pre>func (e <a href="index.html#EscapeError">EscapeError</a>) Error() <a href="../../builtin/index.html#string">string</a></pre>
  203. <h2 id="InvalidHostError">type <a href="http://localhost:6060/src/net/url/url.go?s=1447:1475#L76">InvalidHostError</a></h2>
  204. <pre>type InvalidHostError <a href="../../builtin/index.html#string">string</a></pre>
  205. <h3 id="InvalidHostError.Error">func (InvalidHostError) <a href="http://localhost:6060/src/net/url/url.go?s=1477:1517#L78">Error</a></h3>
  206. <pre>func (e <a href="index.html#InvalidHostError">InvalidHostError</a>) Error() <a href="../../builtin/index.html#string">string</a></pre>
  207. <h2 id="URL">type <a href="http://localhost:6060/src/net/url/url.go?s=8162:8553#L299">URL</a></h2>
  208. <pre>type URL struct {
  209. Scheme <a href="../../builtin/index.html#string">string</a>
  210. Opaque <a href="../../builtin/index.html#string">string</a> <span class="comment">// encoded opaque data</span>
  211. User *<a href="index.html#Userinfo">Userinfo</a> <span class="comment">// username and password information</span>
  212. Host <a href="../../builtin/index.html#string">string</a> <span class="comment">// host or host:port</span>
  213. Path <a href="../../builtin/index.html#string">string</a>
  214. RawPath <a href="../../builtin/index.html#string">string</a> <span class="comment">// encoded path hint (Go 1.5 and later only; see EscapedPath method)</span>
  215. RawQuery <a href="../../builtin/index.html#string">string</a> <span class="comment">// encoded query values, without &#39;?&#39;</span>
  216. Fragment <a href="../../builtin/index.html#string">string</a> <span class="comment">// fragment for references, without &#39;#&#39;</span>
  217. }</pre>
  218. <p>
  219. A URL represents a parsed URL (technically, a URI reference).
  220. The general form represented is:
  221. </p>
  222. <pre>scheme://[userinfo@]host/path[?query][#fragment]
  223. </pre>
  224. <p>
  225. URLs that do not start with a slash after the scheme are interpreted as:
  226. </p>
  227. <pre>scheme:opaque[?query][#fragment]
  228. </pre>
  229. <p>
  230. Note that the Path field is stored in decoded form: /%47%6f%2f becomes /Go/.
  231. A consequence is that it is impossible to tell which slashes in the Path were
  232. slashes in the raw URL and which were %2f. This distinction is rarely important,
  233. but when it is, code must not use Path directly.
  234. </p>
  235. <p>
  236. Go 1.5 introduced the RawPath field to hold the encoded form of Path.
  237. The Parse function sets both Path and RawPath in the URL it returns,
  238. and URL&#39;s String method uses RawPath if it is a valid encoding of Path,
  239. by calling the EscapedPath method.
  240. </p>
  241. <p>
  242. In earlier versions of Go, the more indirect workarounds were that an
  243. HTTP server could consult req.RequestURI and an HTTP client could
  244. construct a URL struct directly and set the Opaque field instead of Path.
  245. These still work as well.
  246. </p>
  247. <div id="example_URL" class="toggle">
  248. <div class="collapsed">
  249. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  250. </div>
  251. <div class="expanded">
  252. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  253. <p>Code:</p>
  254. <pre class="code">u, err := url.Parse(&#34;http://bing.com/search?q=dotnet&#34;)
  255. if err != nil {
  256. log.Fatal(err)
  257. }
  258. u.Scheme = &#34;https&#34;
  259. u.Host = &#34;google.com&#34;
  260. q := u.Query()
  261. q.Set(&#34;q&#34;, &#34;golang&#34;)
  262. u.RawQuery = q.Encode()
  263. fmt.Println(u)
  264. <span class="comment"></pre>
  265. <p>Output:</p>
  266. <pre class="output">https://google.com/search?q=golang
  267. </pre>
  268. </div>
  269. </div>
  270. <div id="example_URL_opaque" class="toggle">
  271. <div class="collapsed">
  272. <p class="exampleHeading toggleButton"><span class="text">Example (Opaque)</span></p>
  273. </div>
  274. <div class="expanded">
  275. <p class="exampleHeading toggleButton"><span class="text">Example (Opaque)</span></p>
  276. <p>Code:</p>
  277. <pre class="code"><span class="comment">// Sending a literal &#39;%&#39; in an HTTP request&#39;s Path</span>
  278. req := &amp;http.Request{
  279. Method: &#34;GET&#34;,
  280. Host: &#34;example.com&#34;, <span class="comment">// takes precedence over URL.Host</span>
  281. URL: &amp;url.URL{
  282. Host: &#34;ignored&#34;,
  283. Scheme: &#34;https&#34;,
  284. Opaque: &#34;/%2f/&#34;,
  285. },
  286. Header: http.Header{
  287. &#34;User-Agent&#34;: {&#34;godoc-example/0.1&#34;},
  288. },
  289. }
  290. out, err := httputil.DumpRequestOut(req, true)
  291. if err != nil {
  292. log.Fatal(err)
  293. }
  294. fmt.Println(strings.Replace(string(out), &#34;\r&#34;, &#34;&#34;, -1))
  295. <span class="comment"></pre>
  296. <p>Output:</p>
  297. <pre class="output">GET /%2f/ HTTP/1.1
  298. Host: example.com
  299. User-Agent: godoc-example/0.1
  300. Accept-Encoding: gzip
  301. </pre>
  302. </div>
  303. </div>
  304. <div id="example_URL_roundtrip" class="toggle">
  305. <div class="collapsed">
  306. <p class="exampleHeading toggleButton"><span class="text">Example (Roundtrip)</span></p>
  307. </div>
  308. <div class="expanded">
  309. <p class="exampleHeading toggleButton"><span class="text">Example (Roundtrip)</span></p>
  310. <p>Code:</p>
  311. <pre class="code"><span class="comment">// Parse + String preserve the original encoding.</span>
  312. u, err := url.Parse(&#34;https://example.com/foo%2fbar&#34;)
  313. if err != nil {
  314. log.Fatal(err)
  315. }
  316. fmt.Println(u.Path)
  317. fmt.Println(u.RawPath)
  318. fmt.Println(u.String())
  319. <span class="comment"></pre>
  320. <p>Output:</p>
  321. <pre class="output">/foo/bar
  322. /foo%2fbar
  323. https://example.com/foo%2fbar
  324. </pre>
  325. </div>
  326. </div>
  327. <h3 id="Parse">func <a href="http://localhost:6060/src/net/url/url.go?s=11220:11267#L403">Parse</a></h3>
  328. <pre>func Parse(rawurl <a href="../../builtin/index.html#string">string</a>) (url *<a href="index.html#URL">URL</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  329. <p>
  330. Parse parses rawurl into a URL structure.
  331. The rawurl may be relative or absolute.
  332. </p>
  333. <h3 id="ParseRequestURI">func <a href="http://localhost:6060/src/net/url/url.go?s=11894:11951#L423">ParseRequestURI</a></h3>
  334. <pre>func ParseRequestURI(rawurl <a href="../../builtin/index.html#string">string</a>) (url *<a href="index.html#URL">URL</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  335. <p>
  336. ParseRequestURI parses rawurl into a URL structure. It assumes that
  337. rawurl was received in an HTTP request, so the rawurl is interpreted
  338. only as an absolute URI or an absolute path.
  339. The string rawurl is assumed not to have a #fragment suffix.
  340. (Web browsers strip #fragment before sending the URL to a web server.)
  341. </p>
  342. <h3 id="URL.EscapedPath">func (*URL) <a href="http://localhost:6060/src/net/url/url.go?s=16370:16404#L577">EscapedPath</a></h3>
  343. <pre>func (u *<a href="index.html#URL">URL</a>) EscapedPath() <a href="../../builtin/index.html#string">string</a></pre>
  344. <p>
  345. EscapedPath returns the escaped form of u.Path.
  346. In general there are multiple possible escaped forms of any path.
  347. EscapedPath returns u.RawPath when it is a valid escaping of u.Path.
  348. Otherwise EscapedPath ignores u.RawPath and computes an escaped
  349. form on its own.
  350. The String and RequestURI methods use EscapedPath to construct
  351. their results.
  352. In general, code should call EscapedPath instead of
  353. reading u.RawPath directly.
  354. </p>
  355. <h3 id="URL.IsAbs">func (*URL) <a href="http://localhost:6060/src/net/url/url.go?s=22627:22653#L834">IsAbs</a></h3>
  356. <pre>func (u *<a href="index.html#URL">URL</a>) IsAbs() <a href="../../builtin/index.html#bool">bool</a></pre>
  357. <p>
  358. IsAbs reports whether the URL is absolute.
  359. </p>
  360. <h3 id="URL.Parse">func (*URL) <a href="http://localhost:6060/src/net/url/url.go?s=22891:22936#L841">Parse</a></h3>
  361. <pre>func (u *<a href="index.html#URL">URL</a>) Parse(ref <a href="../../builtin/index.html#string">string</a>) (*<a href="index.html#URL">URL</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  362. <p>
  363. Parse parses a URL in the context of the receiver. The provided URL
  364. may be relative or absolute. Parse returns nil, err on parse
  365. failure, otherwise its return value is the same as ResolveReference.
  366. </p>
  367. <h3 id="URL.Query">func (*URL) <a href="http://localhost:6060/src/net/url/url.go?s=24144:24172#L887">Query</a></h3>
  368. <pre>func (u *<a href="index.html#URL">URL</a>) Query() <a href="index.html#Values">Values</a></pre>
  369. <p>
  370. Query parses RawQuery and returns the corresponding values.
  371. </p>
  372. <h3 id="URL.RequestURI">func (*URL) <a href="http://localhost:6060/src/net/url/url.go?s=24336:24369#L894">RequestURI</a></h3>
  373. <pre>func (u *<a href="index.html#URL">URL</a>) RequestURI() <a href="../../builtin/index.html#string">string</a></pre>
  374. <p>
  375. RequestURI returns the encoded path?query or opaque?query
  376. string that would be used in an HTTP request for u.
  377. </p>
  378. <h3 id="URL.ResolveReference">func (*URL) <a href="http://localhost:6060/src/net/url/url.go?s=23440:23485#L855">ResolveReference</a></h3>
  379. <pre>func (u *<a href="index.html#URL">URL</a>) ResolveReference(ref *<a href="index.html#URL">URL</a>) *<a href="index.html#URL">URL</a></pre>
  380. <p>
  381. ResolveReference resolves a URI reference to an absolute URI from
  382. an absolute base URI, per RFC 3986 Section 5.2. The URI reference
  383. may be relative or absolute. ResolveReference always returns a new
  384. URL instance, even if the returned URL is identical to either the
  385. base or reference. If ref is an absolute URL, then ResolveReference
  386. ignores base and returns a copy of ref.
  387. </p>
  388. <div id="example_URL_ResolveReference" class="toggle">
  389. <div class="collapsed">
  390. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  391. </div>
  392. <div class="expanded">
  393. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  394. <p>Code:</p>
  395. <pre class="code">u, err := url.Parse(&#34;../../..//search?q=dotnet&#34;)
  396. if err != nil {
  397. log.Fatal(err)
  398. }
  399. base, err := url.Parse(&#34;http://example.com/directory/&#34;)
  400. if err != nil {
  401. log.Fatal(err)
  402. }
  403. fmt.Println(base.ResolveReference(u))
  404. <span class="comment"></pre>
  405. <p>Output:</p>
  406. <pre class="output">http://example.com/search?q=dotnet
  407. </pre>
  408. </div>
  409. </div>
  410. <h3 id="URL.String">func (*URL) <a href="http://localhost:6060/src/net/url/url.go?s=18579:18608#L652">String</a></h3>
  411. <pre>func (u *<a href="index.html#URL">URL</a>) String() <a href="../../builtin/index.html#string">string</a></pre>
  412. <p>
  413. String reassembles the URL into a valid URL string.
  414. The general form of the result is one of:
  415. </p>
  416. <pre>scheme:opaque?query#fragment
  417. scheme://userinfo@host/path?query#fragment
  418. </pre>
  419. <p>
  420. If u.Opaque is non-empty, String uses the first form;
  421. otherwise it uses the second form.
  422. To obtain the path, String uses u.EscapedPath().
  423. </p>
  424. <p>
  425. In the second form, the following rules apply:
  426. </p>
  427. <pre>- if u.Scheme is empty, scheme: is omitted.
  428. - if u.User is nil, userinfo@ is omitted.
  429. - if u.Host is empty, host/ is omitted.
  430. - if u.Scheme and u.Host are empty and u.User is nil,
  431. the entire scheme://userinfo@host/ is omitted.
  432. - if u.Host is non-empty and u.Path begins with a /,
  433. the form host/path does not add its own /.
  434. - if u.RawQuery is empty, ?query is omitted.
  435. - if u.Fragment is empty, #fragment is omitted.
  436. </pre>
  437. <h2 id="Userinfo">type <a href="http://localhost:6060/src/net/url/url.go?s=9457:9539#L331">Userinfo</a></h2>
  438. <pre>type Userinfo struct {
  439. <span class="comment">// contains filtered or unexported fields</span>
  440. }</pre>
  441. <p>
  442. The Userinfo type is an immutable encapsulation of username and
  443. password details for a URL. An existing Userinfo value is guaranteed
  444. to have a username set (potentially empty, as allowed by RFC 2396),
  445. and optionally a password.
  446. </p>
  447. <h3 id="User">func <a href="http://localhost:6060/src/net/url/url.go?s=8639:8675#L312">User</a></h3>
  448. <pre>func User(username <a href="../../builtin/index.html#string">string</a>) *<a href="index.html#Userinfo">Userinfo</a></pre>
  449. <p>
  450. User returns a Userinfo containing the provided username
  451. and no password set.
  452. </p>
  453. <h3 id="UserPassword">func <a href="http://localhost:6060/src/net/url/url.go?s=9113:9167#L323">UserPassword</a></h3>
  454. <pre>func UserPassword(username, password <a href="../../builtin/index.html#string">string</a>) *<a href="index.html#Userinfo">Userinfo</a></pre>
  455. <p>
  456. UserPassword returns a Userinfo containing the provided username
  457. and password.
  458. This functionality should only be used with legacy web sites.
  459. RFC 2396 warns that interpreting Userinfo this way
  460. &ldquo;is NOT RECOMMENDED, because the passing of authentication
  461. information in clear text (such as URI) has proven to be a
  462. security risk in almost every case where it has been used.&rdquo;
  463. </p>
  464. <h3 id="Userinfo.Password">func (*Userinfo) <a href="http://localhost:6060/src/net/url/url.go?s=9711:9755#L343">Password</a></h3>
  465. <pre>func (u *<a href="index.html#Userinfo">Userinfo</a>) Password() (<a href="../../builtin/index.html#string">string</a>, <a href="../../builtin/index.html#bool">bool</a>)</pre>
  466. <p>
  467. Password returns the password in case it is set, and whether it is set.
  468. </p>
  469. <h3 id="Userinfo.String">func (*Userinfo) <a href="http://localhost:6060/src/net/url/url.go?s=9929:9963#L352">String</a></h3>
  470. <pre>func (u *<a href="index.html#Userinfo">Userinfo</a>) String() <a href="../../builtin/index.html#string">string</a></pre>
  471. <p>
  472. String returns the encoded userinfo information in the standard form
  473. of &#34;username[:password]&#34;.
  474. </p>
  475. <h3 id="Userinfo.Username">func (*Userinfo) <a href="http://localhost:6060/src/net/url/url.go?s=9575:9611#L338">Username</a></h3>
  476. <pre>func (u *<a href="index.html#Userinfo">Userinfo</a>) Username() <a href="../../builtin/index.html#string">string</a></pre>
  477. <p>
  478. Username returns the username.
  479. </p>
  480. <h2 id="Values">type <a href="http://localhost:6060/src/net/url/url.go?s=19545:19576#L692">Values</a></h2>
  481. <pre>type Values map[<a href="../../builtin/index.html#string">string</a>][]<a href="../../builtin/index.html#string">string</a></pre>
  482. <p>
  483. Values maps a string key to a list of values.
  484. It is typically used for query parameters and form values.
  485. Unlike in the http.Header map, the keys in a Values map
  486. are case-sensitive.
  487. </p>
  488. <div id="example_Values" class="toggle">
  489. <div class="collapsed">
  490. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  491. </div>
  492. <div class="expanded">
  493. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  494. <p>Code:</p>
  495. <pre class="code">v := url.Values{}
  496. v.Set(&#34;name&#34;, &#34;Ava&#34;)
  497. v.Add(&#34;friend&#34;, &#34;Jess&#34;)
  498. v.Add(&#34;friend&#34;, &#34;Sarah&#34;)
  499. v.Add(&#34;friend&#34;, &#34;Zoe&#34;)
  500. <span class="comment">// v.Encode() == &#34;name=Ava&amp;friend=Jess&amp;friend=Sarah&amp;friend=Zoe&#34;</span>
  501. fmt.Println(v.Get(&#34;name&#34;))
  502. fmt.Println(v.Get(&#34;friend&#34;))
  503. fmt.Println(v[&#34;friend&#34;])
  504. <span class="comment"></pre>
  505. <p>Output:</p>
  506. <pre class="output">Ava
  507. Jess
  508. [Jess Sarah Zoe]
  509. </pre>
  510. </div>
  511. </div>
  512. <h3 id="ParseQuery">func <a href="http://localhost:6060/src/net/url/url.go?s=20592:20643#L731">ParseQuery</a></h3>
  513. <pre>func ParseQuery(query <a href="../../builtin/index.html#string">string</a>) (m <a href="index.html#Values">Values</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  514. <p>
  515. ParseQuery parses the URL-encoded query string and returns
  516. a map listing the values specified for each key.
  517. ParseQuery always returns a non-nil map containing all the
  518. valid query parameters found; err describes the first decoding error
  519. encountered, if any.
  520. </p>
  521. <h3 id="Values.Add">func (Values) <a href="http://localhost:6060/src/net/url/url.go?s=20144:20182#L717">Add</a></h3>
  522. <pre>func (v <a href="index.html#Values">Values</a>) Add(key, value <a href="../../builtin/index.html#string">string</a>)</pre>
  523. <p>
  524. Add adds the value to key. It appends to any existing
  525. values associated with key.
  526. </p>
  527. <h3 id="Values.Del">func (Values) <a href="http://localhost:6060/src/net/url/url.go?s=20267:20298#L722">Del</a></h3>
  528. <pre>func (v <a href="index.html#Values">Values</a>) Del(key <a href="../../builtin/index.html#string">string</a>)</pre>
  529. <p>
  530. Del deletes the values associated with key.
  531. </p>
  532. <h3 id="Values.Encode">func (Values) <a href="http://localhost:6060/src/net/url/url.go?s=21397:21428#L773">Encode</a></h3>
  533. <pre>func (v <a href="index.html#Values">Values</a>) Encode() <a href="../../builtin/index.html#string">string</a></pre>
  534. <p>
  535. Encode encodes the values into &ldquo;URL encoded&rdquo; form
  536. (&#34;bar=baz&amp;foo=quux&#34;) sorted by key.
  537. </p>
  538. <h3 id="Values.Get">func (Values) <a href="http://localhost:6060/src/net/url/url.go?s=19773:19811#L698">Get</a></h3>
  539. <pre>func (v <a href="index.html#Values">Values</a>) Get(key <a href="../../builtin/index.html#string">string</a>) <a href="../../builtin/index.html#string">string</a></pre>
  540. <p>
  541. Get gets the first value associated with the given key.
  542. If there are no values associated with the key, Get returns
  543. the empty string. To access multiple values, use the map
  544. directly.
  545. </p>
  546. <h3 id="Values.Set">func (Values) <a href="http://localhost:6060/src/net/url/url.go?s=19986:20024#L711">Set</a></h3>
  547. <pre>func (v <a href="index.html#Values">Values</a>) Set(key, value <a href="../../builtin/index.html#string">string</a>)</pre>
  548. <p>
  549. Set sets the key to value. It replaces any existing
  550. values.
  551. </p>
  552. <div id="footer">
  553. Build version go1.6.<br>
  554. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  555. the content of this page is licensed under the
  556. Creative Commons Attribution 3.0 License,
  557. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  558. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  559. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  560. </div>
  561. </div><!-- .container -->
  562. </div><!-- #page -->
  563. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  564. <script type="text/javascript" src="../../../lib/godoc/jquery.js"></script>
  565. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.js"></script>
  566. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.edit.js"></script>
  567. <script type="text/javascript" src="../../../lib/godoc/godocs.js"></script>
  568. </body>
  569. </html>