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.