Browse Source

增加HttpBuildQuery

master v0.1.5
guzeng 3 years ago
parent
commit
0096623df4
2 changed files with 46 additions and 5 deletions
  1. +31
    -5
      map.go
  2. +15
    -0
      map_test.go

+ 31
- 5
map.go View File

@ -2,6 +2,8 @@ package helper
import (
"errors"
"sort"
"strings"
)
func MergeStringMap(x, y map[string]string) map[string]string {
@ -65,13 +67,13 @@ func ToMapStringArray(x interface{}) ([]map[string]string, error) {
/**
* 将map[string]string数组转成map[string]interface数组
*/
func MapStringToInterfaceArr(src []map[string]string)([]map[string]interface{}){
func MapStringToInterfaceArr(src []map[string]string) []map[string]interface{} {
var dest []map[string]interface{}
for i := range src {
m := src[i]
n := make(map[string]interface{})
for k,v := range m {
for k, v := range m {
n[k] = interface{}(v)
}
dest = append(dest, n)
@ -83,11 +85,35 @@ func MapStringToInterfaceArr(src []map[string]string)([]map[string]interface{}){
/**
* 将map[string]string转成map[string]interface
*/
func MapStringToInterface(src map[string]string)(map[string]interface{}){
func MapStringToInterface(src map[string]string) map[string]interface{} {
target := make(map[string]interface{})
for k,v := range src {
for k, v := range src {
target[k] = interface{}(v)
}
return target
}
}
/**
* 对map进行字典序排序
* @param data map[string]string
* @2020/12/28
* @linsen
*/
func HttpBuildQuery(data map[string]string) string {
keySlice := []string{}
for k, _ := range data {
keySlice = append(keySlice, k)
}
sort.Strings(keySlice) //排序字符串
var query string
for _, value := range keySlice {
query += StringJoin(value, "=", data[value], "&")
}
return strings.Trim(query, "&")
}

+ 15
- 0
map_test.go View File

@ -0,0 +1,15 @@
package helper
import (
"testing"
)
func Test_HttpBuildQuery(t *testing.T) {
list := map[string]string{"a": "b", "c": "d", "b": "f"}
ret := HttpBuildQuery(list)
t.Log(ret)
}

Loading…
Cancel
Save