Golang pkg for URL parsing and normalization.
- Parsing URL (GoDoc)
- Normalizing URL (GoDoc)
- Splitting host:port from URL (GoDoc)
- Resolving IP address from URL (GoDoc)
The urlx.Parse() is compatible with the same function from net/url pkg, but has slightly different behavior. It enforces default scheme and favors absolute URLs over relative paths.
github.com/goware/urlx | net/url |
---|---|
urlx.Parse("example.com") |
url.Parse("example.com") |
urlx.Parse("localhost:8080") |
url.Parse("localhost:8080") |
urlx.Parse("user.local:8000/path") |
url.Parse("user.local:8000/path") |
import "github.com/goware/urlx"
func main() {
url, _ := urlx.Parse("example.com")
// url.Scheme == "http"
// url.Host == "example.com"
fmt.Print(url)
// Prints http://example.com
}
The urlx.Normalize() function normalizes the URL using the predefined subset of Purell flags.
import "github.com/goware/urlx"
func main() {
url, _ := urlx.Parse("localhost:80///x///y/z/../././index.html?b=y&a=x#t=20")
normalized, _ := urlx.Normalize(url)
fmt.Print(normalized)
// Prints http://localhost/x/y/index.html?a=x&b=y#t=20
}
The urlx.SplitHostPort() is compatible with the same function from net pkg, but has slightly different behavior. It doesn't remove brackets from [IPv6]
host.
import "github.com/goware/urlx"
func main() {
url, _ := urlx.Parse("localhost:80")
host, port, _ := urlx.SplitHostPort(url)
fmt.Print(host)
// Prints localhost
fmt.Print(port)
// Prints 80
}
The urlx.Resolve() is compatible with ResolveIPAddr() from net.
url, _ := urlx.Parse("localhost")
ip, _ := urlx.Resolve(url)
fmt.Print(ip)
// Prints 127.0.0.1
URLx is licensed under the MIT License.