common.go 903 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package util
  2. import (
  3. "math/rand"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. func StringToInt(id string) int {
  9. if id != "" {
  10. id, err := strconv.Atoi(id)
  11. if err == nil {
  12. return id
  13. }
  14. }
  15. return -1
  16. }
  17. func RandomString(n int) string {
  18. var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  19. rand.Seed(time.Now().Unix())
  20. b := make([]rune, n)
  21. for i := range b {
  22. b[i] = letters[rand.Intn(len(letters))]
  23. }
  24. return string(b)
  25. }
  26. func RandomString2(n int) string {
  27. var letters = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  28. rand.Seed(time.Now().Unix())
  29. b := make([]rune, n)
  30. for i := range b {
  31. b[i] = letters[rand.Intn(len(letters))]
  32. }
  33. return string(b)
  34. }
  35. func StringToIntArray(str string) []int64 {
  36. tmp := strings.Split(str, ",")
  37. var result []int64
  38. for _, t := range tmp {
  39. i, _ := strconv.ParseInt(t, 10, 64)
  40. result = append(result, i)
  41. }
  42. return result
  43. }