How to use the UrlEncode encoding of the Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1, first we can view godoc.com under the Net/url

https://godoc.org/net/url

2, let's take a look at the demo code

Package Mainimport ("CRYPTO/MD5"    "Flag"    "FMT"    "Net/url"    "Strings"    "Time") Func main () {FMT. Println("./timetoken-t 3600-key key-url url"var t int64 var key string var Resurl string flag. Int64Var (&t,"T",0,"expire timestamp") flag. Stringvar(&key,"Key","","Encrypt key") flag. Stringvar(&resurl,"url","","Resource url") flag. Parse() if T = =0|| Key = =""|| Resurl = =""{return} expiretime: = Fmt. Sprintf("%x", Time. now(). Unix() +t) Resuri, perr: = URL. Parse(resurl) if perr! = nil {return} FMT. Println(resuri) Path: = Resuri. Escapedpath() FMT. Println(path) Rawstr: = FMT. Sprintf("%s%s%s", key, Path, Expiretime) FMT. Println(RAWSTR) md5h: = MD5. New() md5h. Write([]byte (RAWSTR)) Sign: = FMT. Sprintf("%x", md5h. Sum(nil)) Sign: = Hex. Encodetostring(md5h. Sum(nil)) var Newurl string If strings. Contains(Resurl,"?") {Newurl = Fmt. Sprintf("%s&sign=%s&t=%s", Resurl, sign, expiretime)} else {newurl = FMT. Sprintf("%s?sign=%s&t=%s", Resurl, sign, expiretime)} FMT. Println(Newurl)}

3, and then the above code, let's analyze the next UrlEncode

1, one method we see is the Func (values) encode method, but it needs to provide the encoding service for variables of the values type. The method that returns values has the Func (U *url) Query () values. This doesn't make sense, because I'm going to encode path.

2, then I looked at the func (Values) Encode method source code, found inside the code with the Func queryescape (s string) string This method, but this method, still can not satisfy the URL encoding service, because the "/" did not do special processing

3, in fact, the use is very simple, when you url.parse the time has been coded, but you want to take the path, will be converted back, so the processing failed.

4, eventually using the URL. Escapedpath () This method, which can encode the path directly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.