gurl

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gurl

gurl is a Go library for inspecting, validating, and modifying URL components. It provides focused helpers for query and fragment parameters, paths, hosts, protocols, file extensions, and base URLs without performing network requests.

Install

Go 1.19 or later is supported.

go get github.com/chengchuu/gurl
import "github.com/chengchuu/gurl"

Usage

value, err := gurl.GetQueryParam("https://example.com/search?q=go&lang=en", "q")
if err != nil {
	log.Fatal(err)
}
fmt.Println(value) // go

Run the complete example with go run ./examples/basic.

The public API includes helpers to:

  • get, set, and delete URL query parameters;
  • get, set, and delete query-like parameters in URL fragments;
  • read and replace paths, hosts, hostnames, and protocols;
  • remove queries and fragments to obtain a base URL;
  • read a file extension from a URL path; and
  • check for a scheme and host with CheckValid, or require an http or https scheme together with a host by using CheckValidHTTPURL.

Functions that return an error pass through failures from Go's URL parser. URL setters serialize their result with net/url; query serialization can normalize ordering and escaping.

Documentation

The examples page renders examples/basic/main.go and example_test.go directly from checked-in Go source. The API page extracts exported declarations and GoDoc from the module-root package.

Development

Run package checks from the repository root:

gofmt -w .
go mod tidy
go test ./...
go test -race ./...
go vet ./...
go build ./...
go run ./examples/basic

Generate and validate the GitHub Pages artifact:

go run ./cmd/sitegen build
go run ./cmd/sitegen validate

Preview the production-like site at http://127.0.0.1:4173/gurl/:

go run ./cmd/sitegen preview

dist/pages is generated and ignored by Git. Update source configuration, GoDoc, templates, or assets and rebuild instead of editing generated Pages files.

License

The Go package and project-authored website code are available under the MIT License. Vendored browser-asset notices are in guides/THIRD_PARTY_NOTICES.md.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func 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

func CheckValidHTTPURL added in v1.1.2

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

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"

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

func GetBaseURL added in v1.1.2

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"

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

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

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

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

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

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

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"

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

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

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

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

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

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

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL