utils.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package utils
  4. import (
  5. "math/rand"
  6. "regexp"
  7. "time"
  8. "unsafe"
  9. )
  10. // VerifyMobileFormat 手机号验证
  11. func VerifyMobileFormat(phone string) bool {
  12. regular := "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$"
  13. reg := regexp.MustCompile(regular)
  14. return reg.MatchString(phone)
  15. }
  16. // RankVCode 最小到最大值之间生成随机数
  17. func RankVCode(min int, max int) int {
  18. rand.Seed(time.Now().UnixNano())
  19. return rand.Intn(max-min) + min
  20. }
  21. // StrToBytes 快读的string转byte
  22. func StrToBytes(s string) []byte {
  23. x := (*[2]uintptr)(unsafe.Pointer(&s))
  24. h := [3]uintptr{x[0], x[1], x[1]}
  25. return *(*[]byte)(unsafe.Pointer(&h))
  26. }
  27. // StrToBytes 快速的byte转string
  28. func BytesToStr(b []byte) string {
  29. return *(*string)(unsafe.Pointer(&b))
  30. }
  31. //根据length获取随机字符串
  32. func RandomString(length int) string {
  33. str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  34. bytes := []byte(str)
  35. result := []byte{}
  36. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  37. for i := 0; i < length; i++ {
  38. result = append(result, bytes[r.Intn(len(bytes))])
  39. }
  40. return string(result)
  41. }