tldify

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2024 License: MIT Imports: 4 Imported by: 1

README

tldify

tldify is a Go package that extends the functionality of the standard net/url package by adding support for parsing URLs into additional fields such as Subdomain, Domain, TLD (Top-Level Domain), and Port. It is designed to simplify URL parsing when you need finer granularity beyond the default URL structure.

This package is inspired by go-tld by @jpillora.

Features

  • Parse URLs into components like subdomain, domain, TLD, and port.
  • Support for ICANN-managed TLDs using golang.org/x/net/publicsuffix.
  • Similar API to net/url for easy integration into existing projects.

Installation

To install tldify, use go get:

go get github.com/cyinnove/tldify

Usage

Here's an example of how to use tldify:

package main

import (
	"fmt"
	"github.com/cyinnove/tldify"
)

func main() {
	url, err := tldify.Parse("http://sub.example.co.uk:8080/path?query=1")
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Println("Subdomain:", url.Subdomain)
	fmt.Println("Domain:", url.Domain)
	fmt.Println("TLD:", url.TLD)
	fmt.Println("Port:", url.Port)
	fmt.Println("ICANN:", url.ICANN)
}

Output:

Subdomain: sub
Domain: example
TLD: co.uk
Port: 8080
ICANN: true

tldify exposes a single main function Parse, which mirrors net/url.Parse but returns an enriched URL struct:

func Parse(s string) (*URL, error)
URL Struct

The URL struct embeds the standard net/url.URL and adds the following fields:

  • Subdomain: The subdomain portion of the URL.
  • Domain: The domain portion of the URL.
  • TLD: The Top-Level Domain (TLD) portion of the URL.
  • Port: The port specified in the URL (if any).
  • ICANN: A boolean indicating whether the TLD is an ICANN-managed public suffix.

Inspiration

This package was inspired by go-tld, a powerful tool for extracting domains and TLDs from URLs.

License

tldify is licensed under the MIT License. See the LICENSE file for more information.

Documentation

Overview

Package tldify provides an extended version of net/url.URL. In addition to the standard URL parsing, tldify.URL includes additional fields for Subdomain, Domain, TLD, Port, and ICANN status, offering more detailed URL analysis.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type URL

type URL struct {
	Subdomain string // The subdomain part of the URL (e.g., "sub" in "sub.example.com").
	Domain    string // The registered domain name (e.g., "example" in "example.com").
	TLD       string // The top-level domain (TLD) (e.g., "com" in "example.com").
	Port      string // The port number if specified (e.g., "8080" in "example.com:8080").
	ICANN     bool   // Indicates if the TLD is recognized by ICANN.
	*url.URL         // Embeds net/url.URL to retain the standard URL structure.
}

URL is an extended version of net/url.URL. It contains additional fields such as Subdomain, Domain, TLD, Port, and ICANN to store more detailed information about the parsed URL.

func Parse

func Parse(s string) (*URL, error)

Parse is an extended version of net/url.Parse. It parses the given URL string and returns a tldify.URL containing the standard URL fields along with additional details like Subdomain, Domain, TLD, Port, and ICANN status.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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