Skip to content

Instantly share code, notes, and snippets.

@JesseYan
Last active July 11, 2018 04:04
Show Gist options
  • Save JesseYan/fdc545146ca432e263c1e27a3fd6347c to your computer and use it in GitHub Desktop.
Save JesseYan/fdc545146ca432e263c1e27a3fd6347c to your computer and use it in GitHub Desktop.
ksort for go map[string]map
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