url - The Go Programming Language

Golang

Package url

import "net/url"
Overview
Index
Examples

Overview ?

Overview ?

Package url parses URLs and implements query escaping. 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) Parse(ref string) (*URL, error)
    func (u *URL) Query() Values
    func (u *URL) RequestURI() string
    func (u *URL) ResolveReference(ref *URL) *URL
    func (u *URL) String() string
type Userinfo
    func User(username string) *Userinfo
    func UserPassword(username, password string) *Userinfo
    func (u *Userinfo) Password() (string, bool)
    func (u *Userinfo) String() string
    func (u *Userinfo) Username() string
type Values
    func ParseQuery(query string) (m Values, err error)
    func (v Values) Add(key, value string)
    func (v Values) Del(key string)
    func (v Values) Encode() string
    func (v Values) Get(key string) string
    func (v Values) Set(key, value string)

Examples

URL
Values

Package files

url.go

func QueryEscape

func QueryEscape(s string) string

QueryEscape escapes the string so it can be safely placed inside a URL query.

func QueryUnescape

func QueryUnescape(s string) (string, error)

QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits.

type Error

type Error struct {
    Op  string
    URL string
    Err error
}

Error reports an error and the operation and URL that caused it.

func (*Error) Error

func (e *Error) Error() string

type EscapeError

type EscapeError string

func (EscapeError) Error

func (e EscapeError) Error() string

type URL

type URL struct {
    Scheme   string
    Opaque   string    // encoded opaque data
    User     *Userinfo // username and password information
    Host     string
    Path     string
    RawQuery string // encoded query values, without '?'
    Fragment string // fragment for references, without '#'
}

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]

? Example

? Example

Code:

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)

Output:

https://google.com/search?q=golang

func Parse

func Parse(rawurl string) (url *URL, err error)

Parse parses rawurl into a URL structure. The rawurl may be relative or absolute.

func ParseRequestURI

func ParseRequestURI(rawurl string) (url *URL, err error)

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) IsAbs

func (u *URL) IsAbs() bool

IsAbs returns true if the URL is absolute.

func (*URL) Parse

func (u *URL) Parse(ref string) (*URL, error)

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.

func (*URL) Query

func (u *URL) Query() Values

Query parses RawQuery and returns the corresponding values.

func (*URL) RequestURI

func (u *URL) RequestURI() string

RequestURI returns the encoded path?query or opaque?query string that would be used in an HTTP request for u.

func (*URL) ResolveReference

func (u *URL) ResolveReference(ref *URL) *URL

ResolveReference resolves a URI reference to an absolute URI from an absolute base URI, per RFC 2396 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.

func (*URL) String

func (u *URL) String() string

String reassembles the URL into a valid URL string.

type Userinfo

type Userinfo struct {
    // contains filtered or unexported fields
}

The Userinfo type is an immutable encapsulation of username and password details for a URL. An existing Userinfo value is guaranteed to have a username set (potentially empty, as allowed by RFC 2396), and optionally a password.

func User

func User(username string) *Userinfo

User returns a Userinfo containing the provided username and no password set.

func UserPassword

func UserPassword(username, password string) *Userinfo

UserPassword returns a Userinfo containing the provided username and password. 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.”

func (*Userinfo) Password

func (u *Userinfo) Password() (string, bool)

Password returns the password in case it is set, and whether it is set.

func (*Userinfo) String

func (u *Userinfo) String() string

String returns the encoded userinfo information in the standard form of "username[:password]".

func (*Userinfo) Username

func (u *Userinfo) Username() string

Username returns the username.

type Values

type Values map[string][]string

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.

? Example

? Example

Code:

v := url.Values{}
v.Set("name", "Ava")
v.Add("friend", "Jess")
v.Add("friend", "Sarah")
v.Add("friend", "Zoe")
// v.Encode() == "name=Ava&friend=Jess&friend=Sarah&friend=Zoe"
fmt.Println(v.Get("name"))
fmt.Println(v.Get("friend"))
fmt.Println(v["friend"])

Output:

Ava
Jess
[Jess Sarah Zoe]

func ParseQuery

func ParseQuery(query string) (m Values, err error)

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.

func (Values) Add

func (v Values) Add(key, value string)

Add adds the key to value. It appends to any existing values associated with key.

func (Values) Del

func (v Values) Del(key string)

Del deletes the values associated with key.

func (Values) Encode

func (v Values) Encode() string

Encode encodes the values into “URL encoded” form. e.g. "foo=bar&bar=baz"

func (Values) Get

func (v Values) Get(key string) string

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.

func (Values) Set

func (v Values) Set(key, value string)

Set sets the key to value. It replaces any existing values.