util.go 901 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package utils
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "strconv"
  6. "time"
  7. )
  8. func GenCustomerIdentifier() string {
  9. random := rand.New(rand.NewSource(time.Now().UnixNano()))
  10. return fmt.Sprintf("%08v", random.Int31n(100000000))
  11. }
  12. // 生成随机字符串
  13. func GenerateSubString() string {
  14. var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  15. var n = 12
  16. b := make([]rune, n)
  17. for i := range b {
  18. b[i] = letterRunes[rand.New(rand.NewSource(time.Now().UnixNano())).Intn(len(letterRunes))]
  19. }
  20. return string(b)
  21. }
  22. func Interface2String(inter interface{}) string {
  23. switch inter.(type) {
  24. case string:
  25. return inter.(string)
  26. case int:
  27. return strconv.Itoa(inter.(int))
  28. case int64:
  29. return strconv.FormatInt(inter.(int64), 10)
  30. case float64:
  31. return strconv.FormatFloat(inter.(float64), 'f', -1, 64)
  32. case bool:
  33. return strconv.FormatBool(inter.(bool))
  34. }
  35. return ""
  36. }