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.
- 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.
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.
Below are examples demonstrating how to use the different features of the hq-go-url
package.
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)
}
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
, ororg
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
orrootdomain
root domain.
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)
}
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
, orftp
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
orexample.com
.
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)
}
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"))
We welcome contributions! Feel free to submit Pull Requests or report Issues. For more details, check out the contribution guidelines.
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.
A huge thanks to all the contributors who have helped make hq-go-url
what it is today!
If you're interested in more packages like this, check out: