Follow a declaration link or browse the complete reference below. Signatures and prose come from the current public package source.
Function
CheckValid
func CheckValid(u string) bool
CheckValid checks whether a URL has a non-empty scheme and hostname.
Parameters:
u: The URL to check.
Returns:
A boolean indicating whether the URL is valid.
Example:
result := CheckValid("http://example.com/path/to/resource")
fmt.Println(result) // Output: true
Function
CheckValidHTTPURL
func CheckValidHTTPURL(u string) bool
CheckValidHTTPURL checks if a URL has an HTTP or HTTPS scheme and a non-empty hostname.
Parameters:
u: The URL to check.
Returns:
A boolean indicating whether the URL is a valid HTTP or HTTPS URL.
Example:
result := CheckValidHTTPURL("http://example.com/path/to/resource")
fmt.Println(result) // Output: true
Function
DelHashParam
func DelHashParam(u, param string) (string, error)
DelHashParam deletes a specified hash parameter from a URL and returns the new URL.
Parameters:
u: The URL from which to delete the hash parameter.
param: The name of the hash parameter to delete.
Returns:
A string containing the new URL, and an error if any occurred.
Example:
result, err := DelHashParam("http://example.com/#?t1=1&t2=2", "t1")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "http://example.com/#?t2=2"
Function
DelQueryParam
func DelQueryParam(u, param string) (string, error)
DelQueryParam deletes a specified query parameter from a URL and returns the new URL.
Parameters:
u: The URL from which to delete the query parameter.
param: The name of the query parameter to delete.
Returns:
A string containing the new URL, and an error if any occurred.
Example:
result, err := DelQueryParam("http://example.com/?p1=1&p2=2", "p1")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "http://example.com/?p2=2"
Function
GetBaseURL
func GetBaseURL(u string) (string, error)
GetBaseURL retrieves the base URL without query parameters and fragment.
Parameters:
u: The URL from which to retrieve the base URL.
Returns:
A string containing the base URL, and an error if any occurred.
Example:
result, err := GetBaseURL("https://example.com/path/to/resource?param=value#fragment")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "https://example.com/path/to/resource"
Function
GetHashParam
func GetHashParam(u, param string) (string, error)
GetHashParam retrieves the value of a specified hash parameter from a URL.
It preserves any additional equals signs in the parameter value.
Parameters:
u: The URL from which to retrieve the hash parameter.
param: The name of the hash parameter to retrieve.
Returns:
A string containing the value of the hash parameter, and an error if any occurred.
Example:
result, err := GetHashParam("http://example.com/#?t1=1&t2=2", "t1")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "1"
Function
GetHost
func GetHost(u string) (string, error)
GetHost retrieves the host from a URL.
Parameters:
u: The URL from which to retrieve the host.
Returns:
A string containing the host, and an error if any occurred.
Example:
result, err := GetHost("http://example.com:8080/path/to/resource")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "example.com:8080"
Function
GetHostname
func GetHostname(u string) (string, error)
GetHostname retrieves the hostname from a URL, without square brackets or a port.
Parameters:
u: The URL from which to retrieve the hostname.
Returns:
A string containing the hostname, and an error if any occurred.
Example:
result, err := GetHostname("http://subdomain.example.com:8080/path/to/resource")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "subdomain.example.com"
Function
GetPath
func GetPath(u string) (string, error)
GetPath retrieves the path from a URL.
Parameters:
u: The URL from which to retrieve the path.
Returns:
A string containing the path, and an error if any occurred.
Example:
result, err := GetPath("http://example.com/path/to/resource")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "/path/to/resource"
Function
GetProtocol
func GetProtocol(u string) (string, error)
GetProtocol retrieves the protocol from a URL.
Parameters:
u: The URL from which to retrieve the protocol.
Returns:
A string containing the protocol, and an error if any occurred.
Example:
result, err := GetProtocol("http://example.com/path/to/resource")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "http"
Function
GetQueryParam
func GetQueryParam(u, param string) (string, error)
GetQueryParam retrieves the value of a specified query parameter from a URL.
Parameters:
u: The URL from which to retrieve the query parameter.
param: The name of the query parameter to retrieve.
Returns:
A string containing the value of the query parameter, and an error if any occurred.
Example:
result, err := GetQueryParam("http://example.com/?p1=1&p2=2", "p1")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "1"
Function
GetURLFileType
func GetURLFileType(u string) (string, error)
GetURLFileType retrieves the file type from a URL.
Parameters:
u: The URL from which to retrieve the file type.
Returns:
A string containing the file type, and an error if any occurred.
Example:
result, err := GetURLFileType("https://example.com/a/b/c.png")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "png"
Function
SetHashParam
func SetHashParam(u, param, value string) (string, error)
SetHashParam sets the value of a specified hash parameter in a URL and returns the new URL.
Parameters:
u: The URL in which to set the hash parameter.
param: The name of the hash parameter to set.
value: The value to set the hash parameter to.
Returns:
A string containing the new URL, and an error if any occurred.
Example:
result, err := SetHashParam("http://example.com/#?t1=1&t2=2", "t1", "3")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "http://example.com/#?t1=3&t2=2"
Function
SetHost
func SetHost(u, newHost string) (string, error)
SetHost sets the host in a URL and returns the new URL.
Parameters:
u: The URL in which to set the host.
newHost: The host to set.
Returns:
A string containing the new URL, and an error if any occurred.
Example:
result, err := SetHost("http://example.com:8080/path/to/resource", "newhost.com:9090")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "http://newhost.com:9090/path/to/resource"
Function
SetHostname
func SetHostname(u, newHostname string) (string, error)
SetHostname sets the hostname in a URL and returns the new URL.
It preserves an existing port and formats IPv6 hostnames with square brackets.
Parameters:
u: The URL in which to set the hostname.
newHostname: The hostname to set.
Returns:
A string containing the new URL, and an error if any occurred.
Example:
result, err := SetHostname("http://subdomain.example.com:8080/path/to/resource", "newsubdomain.example.com")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "http://newsubdomain.example.com:8080/path/to/resource"
Function
SetPath
func SetPath(u, newPath string) (string, error)
SetPath sets the path in a URL and returns the new URL.
Parameters:
u: The URL in which to set the path.
newPath: The path to set.
Returns:
A string containing the new URL, and an error if any occurred.
Example:
result, err := SetPath("http://example.com/path/to/resource", "/new/path")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "http://example.com/new/path"
Function
SetProtocol
func SetProtocol(u, newProtocol string) (string, error)
SetProtocol sets the protocol in a URL and returns the new URL.
Parameters:
u: The URL in which to set the protocol.
newProtocol: The protocol to set.
Returns:
A string containing the new URL, and an error if any occurred.
Example:
result, err := SetProtocol("http://example.com/path/to/resource", "https")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "https://example.com/path/to/resource"
Function
SetQueryParam
func SetQueryParam(u, param, value string) (string, error)
SetQueryParam sets the value of a specified query parameter in a URL and returns the new URL.
Parameters:
u: The URL in which to set the query parameter.
param: The name of the query parameter to set.
value: The value to set the query parameter to.
Returns:
A string containing the new URL, and an error if any occurred.
Example:
result, err := SetQueryParam("http://example.com/?p1=1&p2=2", "p1", "3")
if err != nil {
panic(err)
}
fmt.Println(result) // Output: "http://example.com/?p1=3&p2=2"