Documentation
¶
Overview ¶
Package url parses URLs and implements query escaping.
Index ¶
- func JoinPath(base string, elem ...string) (result string, err error)
- func PathEscape(s string) string
- func PathUnescape(s string) (string, error)
- func QueryEscape(s string) string
- func QueryUnescape(s string) (string, error)
- type Error
- type EscapeError
- type InvalidHostError
- type URL
- func (u *URL) AppendBinary(b []byte) ([]byte, error)
- func (u *URL) EscapedFragment() string
- func (u *URL) EscapedPath() string
- func (u *URL) Hostname() string
- func (u *URL) IsAbs() bool
- func (u *URL) JoinPath(elem ...string) *URL
- 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) Redacted() string
- 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
- type Values
Examples ¶
- ParseQuery
- PathEscape
- PathUnescape
- QueryEscape
- QueryUnescape
- URL
- URL (Roundtrip)
- URL.EscapedFragment
- URL.EscapedPath
- URL.Hostname
- URL.IsAbs
- URL.JoinPath
- URL.MarshalBinary
- URL.Parse
- URL.Port
- URL.Query
- URL.Redacted
- URL.RequestURI
- URL.ResolveReference
- URL.String
- URL.UnmarshalBinary
- Values
- Values.Add
- Values.Del
- Values.Encode
- Values.Get
- Values.Has
- Values.Set
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JoinPath ¶ added in go1.19
JoinPath returns a URL string with the provided path elements joined to the existing path of base and the resulting path cleaned of any ./ or ../ elements.
func PathEscape ¶ added in go1.8
PathEscape escapes the string so it can be safely placed inside a URL path segment, replacing special characters (including /) with %XX sequences as needed.
Example ¶
package main
import (
"fmt"
"net/url"
)
func main() {
path := url.PathEscape("my/cool+blog&about,stuff")
fmt.Println(path)
}
Output: my%2Fcool+blog&about%2Cstuff
func PathUnescape ¶ added in go1.8
PathUnescape does the inverse transformation of PathEscape, converting each 3-byte encoded substring of the form "%AB" into the hex-decoded 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).
Example ¶
package main
import (
"fmt"
"log"
"net/url"
)
func main() {
escapedPath := "my%2Fcool+blog&about%2Cstuff"
path, err := url.PathUnescape(escapedPath)
if err != nil {
log.Fatal(err)
}
fmt.Println(path)
}
Output: my/cool+blog&about,stuff
func QueryEscape ¶
QueryEscape escapes the string so it can be safely placed inside a URL query.
Example ¶
package main
import (
"fmt"
"net/url"
)
func main() {
query := url.QueryEscape("my/cool+blog&about,stuff")
fmt.Println(query)
}
Output: my%2Fcool%2Bblog%26about%2Cstuff
func QueryUnescape ¶
QueryUnescape does the inverse transformation of QueryEscape, converting each 3-byte encoded substring of the form "%AB" into the hex-decoded byte 0xAB. It returns an error if any % is not followed by two hexadecimal digits.
Example ¶
package main
import (
"fmt"
"log"
"net/url"
)
func main() {
escapedQuery := "my%2Fcool%2Bblog%26about%2Cstuff"
query, err := url.QueryUnescape(escapedQuery)
if err != nil {
log.Fatal(err)
}
fmt.Println(query)
}
Output: my/cool+blog&about,stuff
Types ¶
type EscapeError ¶
type EscapeError string
func (EscapeError) Error ¶
func (e EscapeError) Error() string
type InvalidHostError ¶ added in go1.6
type InvalidHostError string
func (InvalidHostError) Error ¶ added in go1.6
func (e InvalidHostError) Error() 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 (see Hostname and Port methods)
Path string // path (relative paths may omit leading slash)
RawPath string // encoded path hint (see EscapedPath method)
OmitHost