Skip to content

hueristiq/url

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

url

A Golang package for URL parsing and normalization.

  1. Parsing URL
  2. Normalizing URL

Parsing URL

Difference between github.com/hueristiq/url and net/url

github.com/hueristiq/url net/url
url.Parse("example.com")

&url.URL{ Scheme: "http", Host: "example.com", Path: "", }

url.Parse("example.com")

&url.URL{ Scheme: "", Host: "", Path: "example.com", }

url.Parse("localhost:8080")

&url.URL{ Scheme: "http", Host: "localhost:8080", Path: "", Opaque: "", }

url.Parse("localhost:8080")

&url.URL{ Scheme: "localhost", Host: "", Path: "", Opaque: "8080", }

url.Parse("user.local:8000/path")

&url.URL{ Scheme: "http", Host: "user.local:8000", Path: "/path", Opaque: "", }

url.Parse("user.local:8000/path")

&url.URL{ Scheme: "user.local", Host: "", Path: "", Opaque: "8000/path", }

Usage

import "github.com/hueristiq/url"

func main() {
    url, _ := url.Parse("example.com")
    // url.Scheme == "http"
    // url.Host == "example.com"

    fmt.Print(url)
    // Prints http://example.com
}

Normalizing URL

The url.Normalize() function normalizes the URL using the predefined subset of Purell flags.

Usage

import "github.com/hueristiq/url"

func main() {
    url, _ := url.Parse("localhost:80///x///y/z/../././index.html?b=y&a=x#t=20")
    normalized, _ := url.Normalize(url)

    fmt.Print(normalized)
    // Prints http://localhost/x/y/index.html?a=x&b=y#t=20
}