url

package standard library
go1.24.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2025 License: BSD-3-Clause Imports: 8 Imported by: 574,176

Documentation

Overview

Package url parses URLs and implements query escaping.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func JoinPath added in go1.19

func JoinPath(base string, elem ...string) (result string, err error)

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

func PathEscape(s string) string

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

func PathUnescape(s string) (string, error)

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

func QueryEscape(s string) string

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

func QueryUnescape(s string) (string, error)

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

func (*Error) Temporary added in go1.6

func (e *Error) Temporary() bool

func (*Error) Timeout added in go1.6

func (e *Error) Timeout() bool

func (*Error) Unwrap added in go1.13

func (e *Error) Unwrap() error

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