常用类型及数据操作方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

119 lines
2.1 KiB

package helper
import (
"errors"
"sort"
"strings"
)
func MergeStringMap(x, y map[string]string) map[string]string {
n := make(map[string]string)
for i, v := range x {
for j, w := range y {
if i == j {
n[i] = w
} else {
if _, ok := n[i]; !ok {
n[i] = v
}
if _, ok := n[j]; !ok {
n[j] = w
}
}
}
}
return n
}
/**
* 转换为map string数组
* 2021/1/5
* gz
*/
func ToMapStringArray(x interface{}) ([]map[string]string, error) {
list, ok := x.([]interface{})
if !ok {
return []map[string]string{}, errors.New("type error")
}
if len(list) < 1 {
return []map[string]string{}, nil
}
var ret []map[string]string = make([]map[string]string, len(list))
var tmp map[string]interface{}
var map_tmp map[string]string
var err error
for key, item := range list {
if tmp, ok = item.(map[string]interface{}); ok {
map_tmp = make(map[string]string)
for k, v := range tmp {
map_tmp[k] = ToStr(v)
}
ret[key] = map_tmp
} else {
err = errors.New("data type error")
break
}
}
return ret, err
}
/**
* 将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 {
n[k] = interface{}(v)
}
dest = append(dest, n)
}
return dest
}
/**
* 将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 {
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, "&")
}