common.go 683 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package util
  2. import (
  3. "math/rand"
  4. "strconv"
  5. "time"
  6. )
  7. func StringToInt(id string) int {
  8. if id != "" {
  9. id, err := strconv.Atoi(id)
  10. if err == nil {
  11. return id
  12. }
  13. }
  14. return -1
  15. }
  16. func RandomString(n int) string {
  17. var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  18. rand.Seed(time.Now().Unix())
  19. b := make([]rune, n)
  20. for i := range b {
  21. b[i] = letters[rand.Intn(len(letters))]
  22. }
  23. return string(b)
  24. }
  25. func RandomString2(n int) string {
  26. var letters = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  27. rand.Seed(time.Now().Unix())
  28. b := make([]rune, n)
  29. for i := range b {
  30. b[i] = letters[rand.Intn(len(letters))]
  31. }
  32. return string(b)
  33. }