libString.go 2.6 KB

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