

url · pkg.go.dev
source link: https://pkg.go.dev/net/url
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Types ¶
type InvalidHostError ¶ added in go1.6
type InvalidHostError string
type URL ¶
type URL struct { Scheme string Opaque string // encoded opaque data User *Userinfo // username and password information Host string // host or host:port Path string // path (relative paths may omit leading slash) RawPath string // encoded path hint (see EscapedPath method) ForceQuery bool // append a query ('?') even if RawQuery is empty RawQuery string // encoded query values, without '?' Fragment string // fragment for references, without '#' RawFragment string // encoded fragment hint (see EscapedFragment method) }
A URL represents a parsed URL (technically, a URI reference).
The general form represented is:
[scheme:][//[userinfo@]host][/]path[?query][#fragment]
URLs that do not start with a slash after the scheme are interpreted as:
scheme:opaque[?query][#fragment]
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 RawPath, an optional field which only gets set if the default encoding is different from Path.
URL's String method uses the EscapedPath method to obtain the path. See the EscapedPath method for more details.
Example ¶Example (Roundtrip) ¶
func Parse ¶
Parse parses rawurl into a URL structure.
The rawurl 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.
func ParseRequestURI ¶
ParseRequestURI parses rawurl into a URL structure. It assumes that rawurl was received in an HTTP request, so the rawurl is interpreted only as an absolute URI or an absolute path. The string rawurl is assumed not to have a #fragment suffix. (Web browsers strip #fragment before sending the URL to a web server.)
func (*URL) EscapedFragment ¶ added in go1.15
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 String method uses EscapedFragment to construct its result. In general, code should call EscapedFragment instead of reading u.RawFragment directly.
Example ¶
func (*URL) EscapedPath ¶ added in go1.5
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 String and RequestURI methods use EscapedPath to construct their results. In general, code should call EscapedPath instead of reading u.RawPath directly.
Example ¶
func (*URL) Hostname ¶ added in go1.8
Hostname returns u.Host, stripping any valid port number if present.
If the result is enclosed in square brackets, as literal IPv6 addresses are, the square brackets are removed from the result.
Example ¶
func (*URL) IsAbs ¶
IsAbs reports whether the URL is absolute. Absolute means that it has a non-empty scheme.
Example ¶
func (*URL) MarshalBinary ¶ added in go1.8
Example ¶
func (*URL) Parse ¶
Parse parses a URL 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 ResolveReference.
Example ¶
func (*URL) Port ¶ added in go1.8
Port returns the port part of u.Host, without the leading colon.
If u.Host doesn't contain a valid numeric port, Port returns an empty string.
Example ¶
func (*URL) Query ¶
Query parses RawQuery and returns the corresponding values. It silently discards malformed value pairs. To check errors use ParseQuery.
Example ¶
func (*URL) Redacted ¶ added in go1.15
Redacted is like String but replaces any password with "xxxxx". Only the password in u.URL is redacted.
Example ¶
func (*URL) RequestURI ¶
RequestURI returns the encoded path?query or opaque?query string that would be used in an HTTP request for u.
Example ¶
func (*URL) ResolveReference ¶
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 URL 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.
Example ¶
func (*URL) String ¶
String reassembles the URL into a valid URL string. The general form of the result is one of:
scheme:opaque?query#fragment scheme://userinfo@host/path?query#fragment
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().
In the second form, the following rules apply:
- if u.Scheme is empty, scheme: is omitted. - if u.User is nil, userinfo@ is omitted. - if u.Host is empty, host/ is omitted. - if u.Scheme and u.Host are empty and u.User is nil, the entire scheme://userinfo@host/ is omitted. - if u.Host is non-empty and u.Path begins with a /, the form host/path does not add its own /. - if u.RawQuery is empty, ?query is omitted. - if u.Fragment is empty, #fragment is omitted.
Example ¶
Recommend
-
40
We’re excited to announce that the codebase for pkg.go.dev is now open source. The repository lives at go.googlesource.com/pkgsite...
-
18
Redirecting godoc.org requests to pkg.go.dev Julie Qiu 15 December 2020 With the introduction of Go modules and the g...
-
9
Home · pkg.go.dev F...
-
16
Pkg.go.dev has a new look! Julie Qiu 10 November 2020 Since launching pkg.go.dev, we’...
-
13
Create a badge Create a badge to link to pkg.go.dev from your project website or README file. Badge URL ...
-
9
Documentation Overview ¶
-
5
Discover Packages golang.org/x/tools
-
11
unsafe · pkg.go.dev type ArbitraryType ¶ ...
-
8
The Go Blog A new search experience on pkg.go.dev Julie Qiu 9 November 2021 We are excited to launch a new search experi...
-
3
pkg.go.dev 上版本如何自动更新? GitHub 有个开源的个人小库 MIT 协议 之前有发不过几个 Release 和相应的 Tag,后来把这些发布的 Release 和 tag 都删除了 现在看 pkg.go.dev 发现版本还是很久之前的那个 请问 最近 push 过去的内容...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK