Documentation
¶
Overview ¶
Package gurl provides helpers for inspecting, validating, and modifying URL strings.
Its functions operate in memory and use Go's URL parser. Query helpers use net/url encoding, while hash-parameter helpers preserve the package's query-like fragment behavior.
Index ¶
- func CheckValid(u string) bool
- func CheckValidHTTPURL(u string) bool
- func DelHashParam(u, param string) (string, error)
- func DelQueryParam(u, param string) (string, error)
- func GetBaseURL(u string) (string, error)
- func GetHashParam(u, param string) (string, error)
- func GetHost(u string) (string, error)
- func GetHostname(u string) (string, error)
- func GetPath(u string) (string, error)
- func GetProtocol(u string) (string, error)
- func GetQueryParam(u, param string) (string, error)
- func GetURLFileType(u string) (string, error)
- func SetHashParam(u, param, value string) (string, error)
- func SetHost(u, newHost string) (string, error)
- func SetHostname(u, newHostname string) (string, error)
- func SetPath(u, newPath string) (string, error)
- func SetProtocol(u, newProtocol string) (string, error)
- func SetQueryParam(u, param, value string) (string, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckValid ¶
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
func CheckValidHTTPURL ¶ added in v1.1.2
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
Example ¶
package main
import (
"fmt"
"github.com/chengchuu/gurl"
)
func main() {
fmt.Println(gurl.CheckValidHTTPURL("https://example.com"))
fmt.Println(gurl.CheckValidHTTPURL("https:"))
}
Output: true false
func DelHashParam ¶
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"
func DelQueryParam ¶
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"
func GetBaseURL ¶ added in v1.1.2
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"
func GetHashParam ¶
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"
func GetHost ¶
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"
func GetHostname ¶
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"
func GetPath ¶
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"
func GetProtocol ¶
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"
func GetQueryParam ¶
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"
Example ¶
package main
import (
"fmt"
"github.com/chengchuu/gurl"
)
func main() {
value, err := gurl.GetQueryParam("https://example.com/search?q=go&lang=en", "q")
if err != nil {
panic(err)
}
fmt.Println(value)
}
Output: go
func GetURLFileType ¶ added in v1.1.2
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"
func SetHashParam ¶
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"
func SetHost ¶
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"
func SetHostname ¶
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"
func SetPath ¶
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"
func SetProtocol ¶
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"
func SetQueryParam ¶
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"
Types ¶
This section is empty.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
sitegen
command
Command sitegen builds, validates, and previews the generated GitHub Pages website.
|
Command sitegen builds, validates, and previews the generated GitHub Pages website. |
|
examples
|
|
|
basic
command
Command basic demonstrates query inspection with the public gurl package.
|
Command basic demonstrates query inspection with the public gurl package. |
|
internal
|
|