This is a creation in Article, where the information may have evolved or changed.
Package URL
Import "Net/url"
The URL package resolves the URL and implements the query's escape code, see RFC 3986.
Index
- Func Queryescape (s string) string
- Func Queryunescape (S string) (string, error)
- Type Error
- Func (e *error) Error () string
- Type Escapeerror
- Func (e escapeerror) Error () string
- Type URL
- Func Parse (Rawurl string) (URL *url, err error)
- Func Parserequesturi (Rawurl string) (URL *url, err error)
- Func (U *url) isabs () bool
- Func (U *url) Query () Values
- Func (U *url) requesturi () string
- Func (U *url) string () string
- Func (U *url) Parse (ref string) (*url, error)
- Func (U *url) resolvereference (ref *url) *url
- Type Userinfo
- Func User (username string) *userinfo
- Func userpassword (username, password string) *userinfo
- Func (U *userinfo) Username () string
- Func (U *userinfo) Password () (string, BOOL)
- Func (U *userinfo) string () string
- Type Values
- Func parsequery (query string) (M Values, err Error)
- Func (v Values) Get (key String) string
- Func (v Values) Set (key, value string)
- Func (v Values) ADD (key, value string)
- Func (v Values) Del (key string)
- Func (v Values) Encode () string
Examples
Package Files
Url.go
Func Queryescape
Func Queryescape (s string) string
The queryescape function transcode s to make it safe to use in URL queries.
Func Queryunescape
Func Queryunescape (S string) (string, error)
The queryunescape function is used to restore a string of Queryescape transcoding. It will change the %ab to byte 0xABand change ' + ' to '. If there is a % followed by two hexadecimal digits, this function returns an error.
Type Error
Op URL ERR Error}
error reports a bug and the URL and action that caused the error to occur.
Func (*error) Error
Func (e *error) Error () string
Type Escapeerror
Type Escapeerror string
Func (Escapeerror) Error
Func (e escapeerror) Error () string
Type URL
Scheme Opaque //Opaque data after encoding User //user name and password information Host host or host:port Path Rawquery //Coded query string, no '? ' Fragment //referenced fragment (document location), no ' # ' }
The URL type represents a parsed URL(or, a URL reference). The URL basic format is as follows:
scheme://[userinfo@]host/path[?query][#fragment]
URLs that are not colon-and-double-slash after scheme are interpreted as the following format:
scheme:opaque[?query][#fragment]
Note that the path field is saved in a decoded format, such as /%47%6f%2f becomes /go/. This causes us to be unable to determine whether the slash in the Path field is from the original URL or the %2fbefore decoding. This difference is not important unless a client must use other programs/functions to parse the original URL or refactor the original URL. At this point, theHTTP server can query the req. RequestUri, while HTTP clients can use url{host: "example.com", Opaque: "//example.com/go%2f"} instead {Host: "example.com", Path: "/go/"}.
Example
Func Parse
Func Parse (Rawurl string) (URL *url, err error)
The parse function parses rawurl as a URL struct,rawurl can be an absolute address or a relative address.
Func Parserequesturi
Func Parserequesturi (Rawurl string) (URL *url, err error)
The Parserequesturi function parses rawurl as a URL struct, and this function assumes that Rawurl is in an HTTP request, This assumes that the parameter is an absolute url or an absolute path, and assumes that the URL does not have a #fragment suffix. (The Web browser will not send the URL to the Web server until the suffix is removed)
Func (*url) isabs
Func (U *url) isabs () bool
The function returns True if the URL is an absolute url .
Func (*url) Query
Func (U *url) Query () Values
The Query method parses the rawquery field and returns the values type key-value pair it represents.
Func (*url) RequestUri
Func (U *url) requesturi () string
The RequestUri method returns the encoded path?query or opaque?query string, which is used in the HTTP request.
Func (*url) String
Func (U *url) string () string
String to refactor the URL into a legitimate URL string.
Func (*url) Parse
Func (U *url) Parse (ref string) (*url, error)
The parse method parses a URLwith u as the context, andref can be an absolute or relative URL.
Failure to parse this method returns nil, err,otherwise the returned result is consistent with resolvereference .
Func (*url) resolvereference
Func (U *url) resolvereference (ref *url) *url
This method complements a URI to an absolute URI based on an absolute Uri , see RFC 3986 section 5.2. The parameter ref can be an absolute URI or a relative URI. resolvereference always returns a new URL instance, even if the instance is exactly the same as u or ref . If ref is an absolute uri, this method ignores the reference URI and returns a copy of ref .
Type Userinfo
//contains hidden or non-exported fields }
The Userinfo type is a non-modifiable encapsulation of the user name and password details of a URL . A real Userinfo value must be guaranteed to have a user name (but according to RFC 2396 can be an empty string) and an optional password.
Func User
Func User (username string) *userinfo
The user function returns a *userinfothat does not have a password set to username .
Func UserPassword
Func userpassword (username, password string) *userinfo
the UserPassword function returns a *userinfowith the user name set to usernameand the password set to password .
This function should be used only for old-fashioned sites, because the risk is high and is not recommended, see RFC 2396.
Func (*userinfo) Username
Func (U *userinfo) Username () string
The Username method returns the user name.
Func (*userinfo) Password
Func (U *userinfo) Password () (string, BOOL)
If you set a password to return the password and true, it will return false.
Func (*userinfo) String
Func (U *userinfo) string () string
The String method returns the encoded user information in the format "username[:p assword]".
Type Values
Type Values map[string][]string
values will be mapped to a list of values. It is typically used for querying the parameters and properties of the form. is different from http. Header This dictionary type, the key ofValues is case-sensitive.
Example
Func parsequery
Func parsequery (query string) (M Values, err Error)
The parsequery function parses a URL -encoded query string and returns a dictionary of the Values type that can represent the query. This function always returns a non- nil dictionary that contains all the valid query parameters, anderr is used to describe the first error (if any) encountered while decoding.
Func (Values) Get
Func (v Values) Get (key String) string
Get gets the first value of the value set corresponding to the key . If there is no value for the key , the assembly returns an empty string. To get a set of values, use mapdirectly.
Func (Values) Set
Func (v Values) Set (key, value string)
The Set method sets the set of values corresponding to key to only value, which replaces the existing set of values.
Func (Values) Add
Func (v Values) ADD (key, value string)
Add adds value to the value set that is associated with the key after the original value.
Func (Values) Del
Func (v Values) Del (key string)
Del Deletes the value set associated with the key .
Func (Values) Encode
Func (v Values) Encode () string
The Encode method encodes the v into a URL -encoded format ("Bar=baz&foo=quux"), which is sorted by key when encoded.