URL parser in Golang
The URL parser helps to collect all arguments data from URL.
Example
package main
import (
"fmt"
"log"
"net"
"net/url"
"strings"
)
func main() {
var links = []string{"https://analytics.google.com/analytics/web/#embed/report-home/a98705171w145119383p149829595/",
"jdbc:mysql://test_user:ouupppssss@localhost:3306/sakila?profileSQL=true",
"https://bob:pass@testing.com/country/state",
"http://www.golangprograms.com/",
"mailto:John.Mark@testing.com",
"https://www.google.com/search?q=golang+print+string+10+times&oq=golang+print+string+10+times&aqs=chrome..69i57.8786j0j8&sourceid=chrome&ie=UTF-8",
"urn:oasis:names:description:docbook:dtd:xml:4.1.2",
"https://stackoverflow.com/jobs?med=site-ui&ref=jobs-tab",
"ssh://mark@testing.com",
}
for _, link := range links {
fmt.Println("URL:", link)
u, err := url.Parse(link)
if err != nil {
log.Println(err)
continue
}
parserURL(u)
fmt.Println(strings.Repeat("#", 50))
fmt.Println()
}
}
func parserURL(u *url.URL) {
fmt.Println("Scheme:", u.Scheme)
if u.Opaque != "" {
fmt.Println("Opaque:", u.Opaque)
}
if u.User != nil {
fmt.Println("Username:", u.User.Username())
if pwd, ok := u.User.Password(); ok {
fmt.Println("Password:", pwd)
}
}
if u.Host != "" {
if host, port, err := net.SplitHostPort(u.Host); err == nil {
fmt.Println("Host:", host)
fmt.Println("Port:", port)
} else {
fmt.Println("Host:", u.Host)
}
}
if u.Path != "" {
fmt.Println("Path:", u.Path)
}
if u.RawQuery != "" {
fmt.Println("RawQuery:", u.RawQuery)
m, err := url.ParseQuery(u.RawQuery)
if err == nil {
for k, v := range m {
fmt.Printf("Key: %q Values: %q\n", k, v)
}
}
}
if u.Fragment != "" {
fmt.Println("Fragment:", u.Fragment)
}
}
Output
URL: https://analytics.google.com/analytics/web/#embed/report-home/a98705171w145119383p149829595/
Scheme: https
Host: analytics.google.com
Path: /analytics/web/
Fragment: embed/report-home/a98705171w145119383p149829595/
##################################################
URL: jdbc:mysql://test_user:ouupppssss@localhost:3306/sakila?profileSQL=true
Scheme: jdbc
Opaque: mysql://test_user:ouupppssss@localhost:3306/sakila
RawQuery: profileSQL=true
Key: "profileSQL" Values: ["true"]
##################################################
URL: https://bob:pass@testing.com/country/state
Scheme: https
Username: bob
Password: pass
Host: testing.com
Path: /country/state
##################################################
URL: http://www.golangprograms.com/
Scheme: http
Host: www.golangprograms.com
Path: /
##################################################
URL: mailto:John.Mark@testing.com
Scheme: mailto
Opaque: John.Mark@testing.com
##################################################
URL: https://www.google.com/search?q=golang+print+string+10+times&oq=golang+print+string+10+times&aqs=chrome..69i57.8786j0j8&sourceid=chrome&i
e=UTF-8
Scheme: https
Host: www.google.com
Path: /search
RawQuery: q=golang+print+string+10+times&oq=golang+print+string+10+times&aqs=chrome..69i57.8786j0j8&sourceid=chrome&ie=UTF-8
Key: "q" Values: ["golang print string 10 times"]
Key: "oq" Values: ["golang print string 10 times"]
Key: "aqs" Values: ["chrome..69i57.8786j0j8"]
Key: "sourceid" Values: ["chrome"]
Key: "ie" Values: ["UTF-8"]
##################################################
URL: urn:oasis:names:description:docbook:dtd:xml:4.1.2
Scheme: urn
Opaque: oasis:names:description:docbook:dtd:xml:4.1.2
##################################################
URL: https://stackoverflow.com/jobs?med=site-ui&ref=jobs-tab
Scheme: https
Host: stackoverflow.com
Path: /jobs
RawQuery: med=site-ui&ref=jobs-tab
Key: "med" Values: ["site-ui"]
Key: "ref" Values: ["jobs-tab"]
##################################################
URL: ssh://mark@testing.com
Scheme: ssh
Username: mark
Host: testing.com
##################################################
Most Helpful This Week
Regular expression to extract filename from given path in Golang
How to fix race condition using Atomic Functions in Golang?
Regular expression to validate email address
How to iterate over an Array using for loop?
How to convert Colorful PNG image to Gray-scale?
Sample program to create csv and write data