/hq-go-url

A Go (Golang) package for extracting, parsing, and manipulating URLs with ease. This library is useful for developers who need to work with URLs in a structured way.

Primary LanguageGoMIT LicenseMIT

hq-go-url

made with go go report card open issues closed issues license maintenance contribution

hq-go-url is a Go (Golang) package designed for extracting, parsing, and manipulating URLs with ease. This library is useful for developers who need to work with URLs in a structured way.

Resources

Features

  • Flexible Domain Extraction: Extract domains from text using regular expressions.
  • Flexible URL Extraction: Extract URLs from text using regular expressions.
  • Domain Parsing: Parse domains into subdomains, second-level domains, and top-level domains.
  • Extended URL Parsing: Extend the standard net/url package in Go with additional fields and capabilities.

Installation

To install the package, run the following command in your terminal:

go get -v -u github.com/hueristiq/hq-go-url

This command will download and install the hq-go-url package into your Go workspace, making it available for use in your projects.

Usage

Below are examples demonstrating how to use the different features of the hq-go-url package.

Extraction

Domains

package main

import (
	"fmt"
	hqgourl "github.com/hueristiq/hq-go-url"
	"regexp"
)

func main() {
	extractor := hqgourl.NewDomainExtractor()
	text := "Check out this website: https://example.com and send an email to info@example.com."

	regex := extractor.CompileRegex()
	matches := regex.FindAllString(text, -1)

	fmt.Println("Found Domain:", matches)
}
Customizing Domain Extractor

You can customize how domains are extracted by specifying URL schemes, hosts, or providing custom regular expression patterns.

  • Extract domains with TLD Pattern:

     extractor := hqgourl.NewDomainExtractor(
     	hqgourl.DomainExtractorWithTLDPattern(`(?:com|net|org)`),
     )

    This configuration will extract only domains with com, net, or org TLDs.

  • Extract domains with Root Domain Pattern:

     extractor := hqgourl.NewDomainExtractor(
     	hqgourl.DomainExtractorWithRootDomainPattern(`(?:example|rootdomain)`), // Custom root domain pattern
     )

    This configuration will extract domains that have example or rootdomain root domain.

URLs

package main

import (
	"fmt"
	hqgourl "github.com/hueristiq/hq-go-url"
	"regexp"
)

func main() {
	extractor := hqgourl.NewExtractor()
	text := "Check out this website: https://example.com and send an email to info@example.com."

	regex := extractor.CompileRegex()
	matches := regex.FindAllString(text, -1)

	fmt.Println("Found URLs:", matches)
}
Customizing URL Extractor

You can customize how URLs are extracted by specifying URL schemes, hosts, or providing custom regular expression patterns.

  • Extract URLs with Schemes Pattern:

     extractor := hqgourl.NewExtractor(
     	hqgourl.ExtractorWithSchemePattern(`(?:https?|ftp)://`),
     )

    This configuration will extract URLs with http, https, or ftp schemes.

  • Extract URLs with Host Pattern:

     extractor := hqgourl.NewExtractor(
     	hqgourl.ExtractorWithHostPattern(`(?:www\.)?example\.com`),
     )

    This configuration will extract URLs that have hosts matching www.example.com or example.com.

Parsing

Domains

package main

import (
	"fmt"

	hqgourl "github.com/hueristiq/hq-go-url"
)

func main() {
	parser := hqgourl.NewDomainParser()

	parsed := parser.Parse("subdomain.example.com")

	fmt.Printf("Subdomain: %s, SLD: %s, TLD: %s\n", parsed.Subdomain, parsed.SLD, parsed.TLD)
}

URLs

package main

import (
	"fmt"

	hqgourl "github.com/hueristiq/hq-go-url"
)

func main() {
	parser := hqgourl.NewParser()

	parsed, err := parser.Parse("https://subdomain.example.com:8080/path/file.txt")
	if err != nil {
		fmt.Println("Error parsing URL:", err)

		return
	}

	fmt.Printf("Scheme: %s\n", parsed.Scheme)
	fmt.Printf("Host: %s\n", parsed.Host)
	fmt.Printf("Hostname: %s\n", parsed.Hostname())
	fmt.Printf("Subdomain: %s\n", parsed.Domain.Subdomain)
	fmt.Printf("SLD: %s\n", parsed.Domain.SLD)
	fmt.Printf("TLD: %s\n", parsed.Domain.TLD)
	fmt.Printf("Port: %s\n", parsed.Port())
	fmt.Printf("Path: %s\n", parsed.Path)
}

Set a default scheme:

parser := hqgourl.NewParser(hqgourl.ParserWithDefaultScheme("https"))

Contributing

We welcome contributions! Feel free to submit Pull Requests or report Issues. For more details, check out the contribution guidelines.

Licensing

This package is licensed under the MIT license. You are free to use, modify, and distribute it, as long as you follow the terms of the license. You can find the full license text in the repository - Full MIT license text.

Credits

Contributors

A huge thanks to all the contributors who have helped make hq-go-url what it is today!

contributors

Similar Projects

If you're interested in more packages like this, check out:

urlxxurls