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

148 lines
2.6 KiB

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