go实现urlencode与urldecode

做微信公众号开发时,前后端分离,需要前端传把URL过来,这边需要urldecode

urldecode与urlencode主要用到go官方的net/url库

func main()  {
        var urlStr string = "https://www.test.com?aaa=bbb&ccc=ddd#123"
        escapeUrl := url.QueryEscape(urlStr)
        fmt.Println("urlencode:",escapeUrl)
        enEscapeUrl, _ := url.QueryUnescape(escapeUrl)
        fmt.Println("urldecode:",enEscapeUrl)
}

公众号示例

package main
 
import(
        "fmt"
      url2  "net/url"
)
 
func GetSignPackage(ctx *gin.Context) {
    timestamp := strconv.FormatInt(int64(time.Now().Unix()), 10)
    code, ticket := common.GetTicket()
    if code != 0 {
        fmt.Println("code=", code)
        response.Fail(ctx, gin.H{}, "微信权限不够")
        return
    }
    nonceStr := util.RandomString(16)
    url,_:=url2.QueryUnescape(ctx.Query("url"))
    jsapiTicket := "jsapi_ticket=" + ticket + "&noncestr=" + nonceStr + "×tamp=" + timestamp + "&url=" + url
    signPackage := SignPackage{
        AppId:     viper.GetString("wx.appid"),
        NonceStr:  nonceStr,
        Timestamp: timestamp,
        Signature: util.SHA1(jsapiTicket),
        Url:       url,
    }
    response.Success(ctx, gin.H{"package": signPackage}, "success")
    return
}

你可能感兴趣的:(go实现urlencode与urldecode)