23 September 2017

function

func PathEscape(s string) string

PathEscape escapes the string so it can be safely placed inside a URL path segment.

func PathUnescape(s string) (string, error)

PathUnescape does the inverse transformation of PathEscape, converting %AB into the byte 0xAB. It returns an error if any % is not followed by two hexadecimal digits.

PathUnescape is identical to QueryUnescape except that it does not unescape '+' to ' ' (space).

func QueryEscape(s string) string

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

fmt.Printf("%v\n", url.QueryEscape("key1=value1"))              // %3Fkey1%3Dvalue1
fmt.Printf("%v\n", url.QueryEscape("?key1=value1"))             // key1%3Dvalue1
fmt.Printf("%v\n", url.QueryEscape("key2=value 2"))             // key2%3Dvalue+2
fmt.Printf("%v\n", url.QueryEscape("key1=value1&key2=value 2")) // key1%3Dvalue1%26key2%3Dvalue+2

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

func (e *Error) Error() string

func (e *Error) Temporary() bool

func (e *Error) Timeout() bool

type EscapeError

func (e EscapeError) Error() string

type InvalidHostError

func (e InvalidHostError) Error() string

type URL

[scheme:][//[userinfo@]host][/]path[?query][#fragment]
scheme:opaque[?query][#fragment]
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 '#'
}
func main() {
      u, _ := url.Parse("http://bing.com/search?q=dotnet")

      u.Scheme = "https"
      u.Host = "google.com"

      q := u.Query()
      q.Set("q", "golang")
      u.RawQuery = q.Encode()

      fmt.Println(u) // https://google.com/search?q=golang
}

func main() {
      u1, _ := url.Parse("https://example.com/foo%2fbar")
      fmt.Println(u1.Path)    // /foo/bar
      fmt.Println(u1.RawPath) // /foo%2fbar
      fmt.Println(u1)         // https://example.com/foo%2fbar

      u2, _ := url.Parse("https://example.com/foo/fbar")
      fmt.Println(u2.Path)    // /foo/bar
      fmt.Println(u2.RawPath) //
      fmt.Println(u2)         // https://example.com/foo/fbar
}
u0, _ := url.Parse("http://xxx.com")
fmt.Printf("%v\n", u0.ForceQuery) // false

u1, _ := url.Parse("http://xxx.com/?")
fmt.Printf("%v\n", u1.ForceQuery) // true

u2, _ := url.Parse("http://xxx.com?")
fmt.Printf("%v\n", u2.ForceQuery) // true

u3, _ := url.Parse("http://xxx.com?a=b")
fmt.Printf("%v\n", u3.ForceQuery) // false

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

Parse parses rawurl into a URL structure.

The rawurl may be relative or absolute.

func ParseRequestURI(rawurl string) (*URL, 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 (u *URL) EscapedPath() string

func (u *URL) Hostname() string

func (u *URL) IsAbs() bool

func (u *URL) MarshalBinary() (text []byte, err error)

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

func (u *URL) Port() string

func (u *URL) Query() Values

func (u *URL) RequestURI() string

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

func (u *URL) String() string

func (u *URL) UnmarshalBinary(text []byte) error

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

type Values map[string][]string

func ParseQuery(query string) (Values, error)

m1, _ := url.ParseQuery("x=1&y=2&y=3;z")
fmt.Println(m1)

m2, _ := url.ParseQuery("x=1&y=2&y=3&z=")
fmt.Println(m2)

fmt.Printf("%v\n", m1.Encode()) // x=1&y=2&y=3&z=
fmt.Printf("%v\n", m2.Encode()) // x=1&y=2&y=3&z=

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)