网站换皮肤了
Golang中的URL解析器
时间:2019-10-29 18:41 浏览:19

摘要

URL解析器有助于从URL收集所有参数数据。

代码

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)
    }
}

结果

# go run example.go
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
##################################################




如果这篇文章对你有所帮助,可以通过下边的“打赏”功能进行小额的打赏。

本网站部分内容来源于互联网,如有侵犯版权请来信告知,我们将立即处理。


来说两句吧