net/urlParse

1、url结构

Scheme://host.domain:port/path/to/resource?query_string#fragment
schemehostdomainportpath/to/resourcequery_stringfragment

2、实例

代码:

func getUrlInfo(){
	siteUrl := "http://www.baidu.com:8080/api/getUrlInfo?id=1&color=blue#temp"
	r, _ := url.Parse(siteUrl)
	fmt.Printf("解析url [%v] \n", r)
	fmt.Printf("协议 [%v] \n", r.Scheme)
	fmt.Printf("主机域名 [%v] \n", r.Hostname())
	fmt.Printf("端口 [%v] \n", r.Port())
	fmt.Printf("路径信息 [%v] \n", r.Path)
	fmt.Printf("请求字符串 [%v] \n", r.Query())
	fmt.Printf("锚点 [%v]", r.Fragment)
}

结果:

解析url [http://www.baidu.com:8080/api/getUrlInfo?id=1&color=blue#temp]
协议 [http]
主机域名 [www.baidu.com]
端口 [8080]
路径信息 [/api/getUrlInfo]
请求字符串 [map[color:[blue] id:[1]]]
锚点 [temp]