convert.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package utils
  2. import (
  3. "container/list"
  4. "encoding/json"
  5. "strings"
  6. "github.com/gogf/gf/container/gvar"
  7. "github.com/gogf/gf/util/gconv"
  8. )
  9. // 将带分隔符的字符串切成int数组
  10. func ToIntArray(str string, split ...string) []int {
  11. splitChar := ","
  12. if len(split) > 0 {
  13. splitChar = split[0]
  14. }
  15. result := make([]int, 0)
  16. if str == "" {
  17. return result
  18. }
  19. arr := strings.Split(str, splitChar)
  20. if len(arr) > 0 {
  21. for i := range arr {
  22. if arr[i] != "" {
  23. result = append(result, gconv.Int(arr[i]))
  24. }
  25. }
  26. }
  27. return result
  28. }
  29. // 将带分隔符的字符串切成int64数组
  30. func ToInt64Array(str string, split ...string) []int64 {
  31. splitChar := ","
  32. if len(split) > 0 {
  33. splitChar = split[0]
  34. }
  35. result := make([]int64, 0)
  36. if str == "" {
  37. return result
  38. }
  39. arr := strings.Split(str, splitChar)
  40. if len(arr) > 0 {
  41. for i := range arr {
  42. if arr[i] != "" {
  43. result = append(result, gconv.Int64(arr[i]))
  44. }
  45. }
  46. }
  47. return result
  48. }
  49. // 将带分隔符的字符串切成string数组
  50. func ToStringArray(str string, split ...string) []string {
  51. if str == "" {
  52. return nil
  53. }
  54. splitChar := ","
  55. if len(split) > 0 {
  56. splitChar = split[0]
  57. }
  58. result := strings.Split(str, splitChar)
  59. return result
  60. }
  61. // 将数据表查询返回的ID数据转换成带分隔符的Ids字符串,形如:1,2,3
  62. func ToIdsString(array []*gvar.Var, split ...string) string {
  63. splitChar := ","
  64. if len(split) > 0 {
  65. splitChar = split[0]
  66. }
  67. result := ""
  68. for _, v := range array {
  69. result = result + v.String() + splitChar
  70. }
  71. result = result[0 : len(result)-1]
  72. return result
  73. }
  74. // 将数据表查询返回的ID数组(整数数组,支持int,int32,int64)数据转换成带分隔符的Ids字符串,形如:1,2,3
  75. func IntArrayToIdsString(array interface{}, split ...string) string {
  76. if array == nil {
  77. return ""
  78. }
  79. splitChar := ","
  80. if len(split) > 0 {
  81. splitChar = split[0]
  82. }
  83. result := ""
  84. switch value := array.(type) {
  85. case []int:
  86. if len(value) == 0 {
  87. return ""
  88. }
  89. for _, v := range value {
  90. result = result + gconv.String(v) + splitChar
  91. }
  92. case []int32:
  93. if len(value) == 0 {
  94. return ""
  95. }
  96. for _, v := range value {
  97. result = result + gconv.String(v) + splitChar
  98. }
  99. case []int64:
  100. if len(value) == 0 {
  101. return ""
  102. }
  103. for _, v := range value {
  104. result = result + gconv.String(v) + splitChar
  105. }
  106. case []string:
  107. if len(value) == 0 {
  108. return ""
  109. }
  110. for _, v := range value {
  111. result = result + v + splitChar
  112. }
  113. }
  114. result = result[0 : len(result)-1]
  115. return result
  116. }
  117. // 将数据表查询返回的ID数据转换成带分隔符的Ids字符串,形如:1,2,3
  118. func Int64ArrayToIdsString(array []int64, split ...string) string {
  119. splitChar := ","
  120. if len(split) > 0 {
  121. splitChar = split[0]
  122. }
  123. result := ""
  124. for _, v := range array {
  125. result = result + gconv.String(v) + splitChar
  126. }
  127. result = result[0 : len(result)-1]
  128. return result
  129. }
  130. // 过滤收尾有分隔符的数字字符串
  131. func ReplaceHeadAndEndStr(str string, split ...string) string {
  132. splitChar := ","
  133. if len(split) > 0 {
  134. splitChar = split[0]
  135. }
  136. result := ""
  137. arr := strings.Split(str, splitChar)
  138. if len(arr) <= 0 {
  139. return result
  140. }
  141. for i := range arr {
  142. if arr[i] != "" {
  143. if i == 0 {
  144. result = arr[i]
  145. } else {
  146. result += "," + arr[i]
  147. }
  148. }
  149. }
  150. return result
  151. }
  152. // ToJsonStr 对象转字符串
  153. func ToJsonString(data interface{}) (string, error) {
  154. result, err := json.Marshal(data)
  155. return string(result), err
  156. }
  157. // Json字符串转对象
  158. func JsonToStruct(source string, destination interface{}) error {
  159. err := json.Unmarshal([]byte(source), destination)
  160. return err
  161. }
  162. // Json字符串转Map
  163. func JsonToMap(source string) (map[string]string, error) {
  164. resultMap := make(map[string]string)
  165. err := json.Unmarshal([]byte(source), &resultMap)
  166. if err != nil {
  167. return nil, err
  168. }
  169. return resultMap, nil
  170. }
  171. // list对象转数组
  172. func ListToArray(list *list.List) []interface{} {
  173. var len = list.Len()
  174. if len == 0 {
  175. return nil
  176. }
  177. var arr []interface{}
  178. for e := list.Front(); e != nil; e = e.Next() {
  179. arr = append(arr, e.Value)
  180. }
  181. return arr
  182. }