一、urlencode

golang下可以使用net/url模块实现urlencode和urldecode操作。具体实现的函数为url.QueryEscape和url.QueryUnescape,代码如下:

 1package main
 2import(
 3    "fmt"
 4    "net/url"
 5)
 6func main()  {
 7    var urlStr string = "运维之路"
 8    escapeUrl := url.QueryEscape(urlStr)
 9    fmt.Println("编码:",escapeUrl)
10    enEscapeUrl, _ := url.QueryUnescape(escapeUrl)
11    fmt.Println("解码:",enEscapeUrl)
12}

如果涉及到多个参数转码的,这在get和post请求中都有,比如id=100&site=361way.com这样的。可以在通过url.Values进行增加后转码,发下:

 1package main
 2import (
 3    "fmt"
 4    "net/url"
 5)
 6func main() {
 7    params := url.Values{}
 8    params.Add("name", "@Rajeev")
 9    params.Add("phone", "+919999999999")
10    fmt.Println(params.Encode())
11}
12#输出
13name=%40Rajeev&phone=%2B919999999999

同样的,我们通过url的其他函数处理操作过后,一个典型的get请求的URL如下:

 1package main
 2import (
 3    "fmt"
 4    "net/url"
 5)
 6func main() {
 7    // Let's start with a base url
 8    baseUrl, err := url.Parse("http://www.mywebsite.com")
 9    if err != nil {
10        fmt.Println("Malformed URL: ", err.Error())
11        return
12    }
13    // Add a Path Segment (Path segment is automatically escaped)
14    baseUrl.Path += "path with?reserved characters"
15    // Prepare Query Parameters
16    params := url.Values{}
17    params.Add("q", "Hello World")
18    params.Add("u", "@rajeev")
19    // Add Query Parameters to the URL
20    baseUrl.RawQuery = params.Encode() // Escape Query Parameters
21    fmt.Printf("Encoded URL is %q\n", baseUrl.String())
22}
23#输出结果
24Encoded URL is "http://www.mywebsite.com/path%20with%3Freserved%20characters?q=Hello+World&u=%40rajeev"

二、urldecode

简单的url解码,直接使用Unescape就可以了,如下:

 1package main
 2import (
 3    "fmt"
 4    "log"
 5    "net/url"
 6)
 7func main() {
 8    encodedValue := "Hell%C3%B6+W%C3%B6rld%40Golang"
 9    decodedValue, err := url.QueryUnescape(encodedValue)
10    if err != nil {
11        log.Fatal(err)
12        return
13    }
14    fmt.Println(decodedValue)
15}

带form 参数,就像上面提到的”param1=value1&param2=value2″这种格式的可以使用url.ParseQuery()参数解析成map[string][]string格式,如下:

 1package main
 2import (
 3    "fmt"
 4    "log"
 5    "net/url"
 6)
 7func main() {
 8    queryStr := "name=Rajeev%20Singh&phone=%2B9199999999&phone=%2B628888888888"
 9    params, err := url.ParseQuery(queryStr)
10    if err != nil {
11        log.Fatal(err)
12        return
13    }
14    fmt.Println("Query Params: ")
15    for key, value := range params {
16        fmt.Printf("  %v = %v\n", key, value)
17    }
18}
19#其输出为
20Query Params:
21  name = [Rajeev Singh]
22  phone = [+9199999999 +628888888888]

一个get请求类,后面带参数的URL,可以通过如下方式解析并解码

 1package main
 2import (
 3    "fmt"
 4    "log"
 5    "net/url"
 6)
 7func main() {
 8    u, err := url.Parse("https://www.website.com/person?name=Rajeev%20Singh&phone=%2B919999999999&phone=%2B628888888888")
 9    if err != nil {
10        log.Fatal(err)
11        return
12    }
13    fmt.Println("Scheme: ", u.Scheme)
14    fmt.Println("Host: ", u.Host)
15    queries := u.Query()
16    fmt.Println("Query Strings: ")
17    for key, value := range queries {
18        fmt.Printf("  %v = %v\n", key, value)
19    }
20    fmt.Println("Path: ", u.Path)
21}
22# 输出如下
23Scheme:  https
24Host:  www.website.com
25Query Strings:
26  phone = [+919999999999 +628888888888]
27  name = [Rajeev Singh]
28Path:  /person