Last active
July 11, 2018 04:04
-
-
Save JesseYan/fdc545146ca432e263c1e27a3fd6347c to your computer and use it in GitHub Desktop.
ksort for go map[string]map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"encoding/hex" | |
"sort" | |
"strings" | |
) | |
//Ksort 按照key进行排序; params: k1=v1&k2=v2 | |
func Ksort(paramsMap map[string]interface{}) string { | |
if len(paramsMap) == 0 { | |
return "" | |
} | |
var params []string | |
var result string | |
keySlice := make([]string, 0) | |
for k := range paramsMap { | |
keySlice = append(keySlice, k) | |
} | |
//sort | |
sort.Strings(keySlice) | |
for _, k := range keySlice { | |
v := Stringfy(paramsMap[k]) | |
// 处理特殊字符 | |
r := strings.NewReplacer(" ", "", "\t", "", "\n", "") | |
v = r.Replace(v) | |
// QueryEscape,类似url-encode功能 | |
params = append(params, k+"="+url.QueryEscape(v)) | |
} | |
result = strings.Join(params, "") | |
log.Logger.Info("result:", result) | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment