libString.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package lib
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "github.com/shopspring/decimal"
  6. "math/rand"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // #取得随机字符串:通过打乱slice来操作
  12. func GetRandstring(length int, char string, rand_x int64) string {
  13. if length < 1 {
  14. return ""
  15. }
  16. if len(char) <= 6 || len(char) <= length {
  17. char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  18. }
  19. charArr := strings.Split(char, "")
  20. ran := rand.New(rand.NewSource(time.Now().Unix() + rand_x))
  21. l := len(charArr)
  22. for i := l - 1; i > 0; i-- {
  23. r := ran.Intn(i)
  24. charArr[r], charArr[i] = charArr[i], charArr[r]
  25. }
  26. rchar := charArr[:length]
  27. return strings.Join(rchar, "")
  28. }
  29. // 返回一个32位md5加密后的字符串
  30. func Md5(str string) string {
  31. h := md5.New()
  32. h.Write([]byte(str))
  33. return hex.EncodeToString(h.Sum(nil))
  34. }
  35. // 转化
  36. func TimeStrToTime(T_time string) (time.Time, bool) {
  37. stamp, err := time.ParseInLocation("2006-01-02 15:04:05", T_time, time.Local) // +8
  38. if err != nil {
  39. return time.Time{}, false
  40. }
  41. return stamp, true
  42. }
  43. func DateStrToTime(T_date string) (time.Time, bool) {
  44. stamp, err := time.ParseInLocation("2006-01-02", T_date, time.Local) // +8
  45. if err != nil {
  46. return time.Time{}, false
  47. }
  48. return stamp, true
  49. }
  50. func MonthStrToTime(T_month string) (time.Time, bool) {
  51. stamp, err := time.ParseInLocation("2006-01", T_month, time.Local) // +8
  52. if err != nil {
  53. return time.Time{}, false
  54. }
  55. return stamp, true
  56. }
  57. func SplitStringIds(str string, prefix string) (r []string) {
  58. if len(str) == 0 {
  59. return r
  60. }
  61. Ids_str := strings.TrimRight(str, "|")
  62. Ids := strings.Split(Ids_str, "|")
  63. for _, v := range Ids {
  64. r = append(r, strings.TrimLeft(v, prefix))
  65. }
  66. return r
  67. }
  68. func SplitStringToDotStr(str string, prefix string) (r string) {
  69. if len(str) == 0 {
  70. return r
  71. }
  72. Ids_str := strings.TrimRight(str, "|")
  73. Ids := strings.Split(Ids_str, "|")
  74. for _, v := range Ids {
  75. r += strings.TrimLeft(v, prefix) + ","
  76. }
  77. r = strings.TrimRight(r, ",")
  78. return r
  79. }
  80. func SplitStringToIntIds(str string, prefix string) (r []int) {
  81. if len(str) == 0 {
  82. return r
  83. }
  84. Ids_str := strings.TrimRight(str, "|")
  85. Ids := strings.Split(Ids_str, "|")
  86. for _, v := range Ids {
  87. id, _ := strconv.Atoi(strings.TrimLeft(v, prefix))
  88. r = append(r, id)
  89. }
  90. return r
  91. }
  92. func IntIdsDistinct(Ids []int) (result []int) {
  93. distinctMap := make(map[int]int, len(Ids))
  94. for _, Id := range Ids {
  95. if _, ok := distinctMap[Id]; !ok {
  96. distinctMap[Id] = 1
  97. result = append(result, Id)
  98. }
  99. }
  100. return result
  101. }
  102. func SplitString(str string, cutset string) (r []string) {
  103. if len(str) == 0 {
  104. return r
  105. }
  106. list := strings.Split(strings.TrimRight(str, cutset), cutset)
  107. return list
  108. }
  109. // string转float64保留2位小数
  110. func StringToFloat64TwoDecimal(str string) float64 {
  111. temp, _ := strconv.ParseFloat(str, 64)
  112. f, _ := decimal.NewFromFloat(temp).Round(2).Float64()
  113. return f
  114. }