golang 的 rabbitmq 消费项目
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

string.go 4.8 KiB

1 anno fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package utils
  2. import (
  3. "fmt"
  4. "github.com/syyongx/php2go"
  5. "reflect"
  6. "sort"
  7. "strings"
  8. )
  9. func Implode(glue string, args ...interface{}) string {
  10. data := make([]string, len(args))
  11. for i, s := range args {
  12. data[i] = fmt.Sprint(s)
  13. }
  14. return strings.Join(data, glue)
  15. }
  16. //字符串是否在数组里
  17. func InArr(target string, str_array []string) bool {
  18. for _, element := range str_array {
  19. if target == element {
  20. return true
  21. }
  22. }
  23. return false
  24. }
  25. func StrToFormat(s string, prec int) string {
  26. ex := strings.Split(s, ".")
  27. if len(ex) == 2 {
  28. if StrToFloat64(ex[1]) == 0 { //小数点后面为空就是不要小数点了
  29. return ex[0]
  30. }
  31. //看取多少位
  32. str := ex[1]
  33. str1 := str
  34. if prec < len(str) {
  35. str1 = str[0:prec]
  36. } else {
  37. for i := 0; i < prec-len(str); i++ {
  38. str1 += "0"
  39. }
  40. }
  41. if prec > 0 {
  42. return ex[0] + "." + str1
  43. } else {
  44. return ex[0]
  45. }
  46. }
  47. return s
  48. }
  49. func GetPrec(sum, commPrec string) string {
  50. if sum == "" {
  51. sum = "0"
  52. }
  53. sum = StrToFormat(sum, StrToInt(commPrec))
  54. ex := strings.Split(sum, ".")
  55. if len(ex) == 2 {
  56. if StrToFloat64(ex[1]) == 0 {
  57. sum = ex[0]
  58. } else {
  59. val := Float64ToStrByPrec(StrToFloat64(ex[1]), 0)
  60. keyMax := 0
  61. for i := 0; i < len(val); i++ {
  62. ch := string(val[i])
  63. fmt.Println(StrToInt(ch))
  64. if StrToInt(ch) > 0 {
  65. keyMax = i
  66. }
  67. }
  68. valNew := val[0 : keyMax+1]
  69. sum = ex[0] + "." + strings.ReplaceAll(ex[1], val, valNew)
  70. }
  71. }
  72. return sum
  73. }
  74. //把数组的值放到key里
  75. func ArrayColumn(array interface{}, key string) (result map[string]interface{}, err error) {
  76. result = make(map[string]interface{})
  77. t := reflect.TypeOf(array)
  78. v := reflect.ValueOf(array)
  79. if t.Kind() != reflect.Slice {
  80. return nil, nil
  81. }
  82. if v.Len() == 0 {
  83. return nil, nil
  84. }
  85. for i := 0; i < v.Len(); i++ {
  86. indexv := v.Index(i)
  87. if indexv.Type().Kind() != reflect.Struct {
  88. return nil, nil
  89. }
  90. mapKeyInterface := indexv.FieldByName(key)
  91. if mapKeyInterface.Kind() == reflect.Invalid {
  92. return nil, nil
  93. }
  94. mapKeyString, err := InterfaceToString(mapKeyInterface.Interface())
  95. if err != nil {
  96. return nil, err
  97. }
  98. result[mapKeyString] = indexv.Interface()
  99. }
  100. return result, err
  101. }
  102. //转string
  103. func InterfaceToString(v interface{}) (result string, err error) {
  104. switch reflect.TypeOf(v).Kind() {
  105. case reflect.Int64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
  106. result = fmt.Sprintf("%v", v)
  107. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  108. result = fmt.Sprintf("%v", v)
  109. case reflect.String:
  110. result = v.(string)
  111. default:
  112. err = nil
  113. }
  114. return result, err
  115. }
  116. func HideTrueName(name string) string {
  117. res := "**"
  118. if name != "" {
  119. runs := []rune(name)
  120. leng := len(runs)
  121. if leng <= 3 {
  122. res = string(runs[0:1]) + res
  123. } else if leng < 5 {
  124. res = string(runs[0:2]) + res
  125. } else if leng < 10 {
  126. res = string(runs[0:2]) + "***" + string(runs[leng-2:leng])
  127. } else if leng < 16 {
  128. res = string(runs[0:3]) + "****" + string(runs[leng-3:leng])
  129. } else {
  130. res = string(runs[0:4]) + "*****" + string(runs[leng-4:leng])
  131. }
  132. }
  133. return res
  134. }
  135. func GetQueryParam(uri string) map[string]string {
  136. //根据问号分割路由还是query参数
  137. uriList := strings.Split(uri, "?")
  138. var query = make(map[string]string, 0)
  139. //有参数才处理
  140. if len(uriList) == 2 {
  141. //分割query参数
  142. var queryList = strings.Split(uriList[1], "&")
  143. if len(queryList) > 0 {
  144. //key value 分别赋值
  145. for _, v := range queryList {
  146. var valueList = strings.Split(v, "=")
  147. if len(valueList) == 2 {
  148. value, _ := php2go.URLDecode(valueList[1])
  149. if value == "" {
  150. value = valueList[1]
  151. }
  152. query[valueList[0]] = value
  153. }
  154. }
  155. }
  156. }
  157. return query
  158. }
  159. //JoinStringsInASCII 按照规则,参数名ASCII码从小到大排序后拼接
  160. //data 待拼接的数据
  161. //sep 连接符
  162. //onlyValues 是否只包含参数值,true则不包含参数名,否则参数名和参数值均有
  163. //includeEmpty 是否包含空值,true则包含空值,否则不包含,注意此参数不影响参数名的存在
  164. //exceptKeys 被排除的参数名,不参与排序及拼接
  165. func JoinStringsInASCII(data map[string]string, sep string, onlyValues, includeEmpty bool, exceptKeys ...string) string {
  166. var list []string
  167. var keyList []string
  168. m := make(map[string]int)
  169. if len(exceptKeys) > 0 {
  170. for _, except := range exceptKeys {
  171. m[except] = 1
  172. }
  173. }
  174. for k := range data {
  175. if _, ok := m[k]; ok {
  176. continue
  177. }
  178. value := data[k]
  179. if !includeEmpty && value == "" {
  180. continue
  181. }
  182. if onlyValues {
  183. keyList = append(keyList, k)
  184. } else {
  185. list = append(list, fmt.Sprintf("%s=%s", k, value))
  186. }
  187. }
  188. if onlyValues {
  189. sort.Strings(keyList)
  190. for _, v := range keyList {
  191. list = append(list, AnyToString(data[v]))
  192. }
  193. } else {
  194. sort.Strings(list)
  195. }
  196. return strings.Join(list, sep)
  197. }