常用类型及数据操作方法
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.

163 lines
3.0 KiB

3 years ago
3 years ago
3 years ago
  1. package helper
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/url"
  6. "sort"
  7. "strings"
  8. )
  9. func MergeStringMap(x, y map[string]string) map[string]string {
  10. n := make(map[string]string)
  11. for i, v := range x {
  12. for j, w := range y {
  13. if i == j {
  14. n[i] = w
  15. } else {
  16. if _, ok := n[i]; !ok {
  17. n[i] = v
  18. }
  19. if _, ok := n[j]; !ok {
  20. n[j] = w
  21. }
  22. }
  23. }
  24. }
  25. return n
  26. }
  27. /**
  28. * 转换为map string数组
  29. * 2021/1/5
  30. * gz
  31. */
  32. func ToMapStringArray(x interface{}) ([]map[string]string, error) {
  33. list, ok := x.([]interface{})
  34. if !ok {
  35. return []map[string]string{}, errors.New("type error")
  36. }
  37. if len(list) < 1 {
  38. return []map[string]string{}, nil
  39. }
  40. var ret []map[string]string = make([]map[string]string, len(list))
  41. var tmp map[string]interface{}
  42. var map_tmp map[string]string
  43. var err error
  44. for key, item := range list {
  45. if tmp, ok = item.(map[string]interface{}); ok {
  46. map_tmp = make(map[string]string)
  47. for k, v := range tmp {
  48. map_tmp[k] = ToStr(v)
  49. }
  50. ret[key] = map_tmp
  51. } else {
  52. err = errors.New("data type error")
  53. break
  54. }
  55. }
  56. return ret, err
  57. }
  58. /**
  59. * 将map[string]string数组转成map[string]interface数组
  60. */
  61. func MapStringToInterfaceArr(src []map[string]string) []map[string]interface{} {
  62. var dest []map[string]interface{}
  63. for i := range src {
  64. m := src[i]
  65. n := make(map[string]interface{})
  66. for k, v := range m {
  67. n[k] = interface{}(v)
  68. }
  69. dest = append(dest, n)
  70. }
  71. return dest
  72. }
  73. /**
  74. * 将map[string]string转成map[string]interface
  75. */
  76. func MapStringToInterface(src map[string]string) map[string]interface{} {
  77. target := make(map[string]interface{})
  78. for k, v := range src {
  79. target[k] = interface{}(v)
  80. }
  81. return target
  82. }
  83. //将interface转成map[string]interface
  84. func InterfaceToMapInterface(data interface{})(map[string]interface{},error){
  85. dataJson,err := json.Marshal(data)
  86. if err != nil {
  87. return nil,err
  88. }
  89. var returnData map[string]interface{}
  90. err = json.Unmarshal(dataJson,&returnData)
  91. return returnData,nil
  92. }
  93. /**
  94. * 对map进行字典序排序
  95. * @param data map[string]string
  96. * @2020/12/28
  97. * @linsen
  98. */
  99. func HttpBuildQuery(data map[string]string) string {
  100. keySlice := []string{}
  101. for k, _ := range data {
  102. keySlice = append(keySlice, k)
  103. }
  104. sort.Strings(keySlice) //排序字符串
  105. var query string
  106. for _, value := range keySlice {
  107. if value != "" && data[value] != "" {
  108. query += StringJoin(value, "=", data[value], "&")
  109. }
  110. }
  111. return strings.Trim(query, "&")
  112. }
  113. /**
  114. * 对map字典按照顺序进行拼接并且把每个value进行类urlencode
  115. * @param data map[string]string
  116. * @2021/10/14
  117. * @wyy
  118. */
  119. func HttpBuildStringQuery(data map[string]string) string {
  120. keySlice := []string{}
  121. for k, _ := range data {
  122. keySlice = append(keySlice, k)
  123. }
  124. // sort.Strings(keySlice) //排序字符串
  125. var query string
  126. for _, value := range keySlice {
  127. if value != "" && data[value] != "" {
  128. query += StringJoin(value, "=", url.QueryEscape(data[value]), "&")
  129. }
  130. }
  131. return strings.Trim(query, "&")
  132. }