utils.go 795 B

1234567891011121314151617181920212223242526272829303132333435
  1. package utils
  2. import (
  3. "fmt"
  4. "regexp"
  5. "unsafe"
  6. "encoding/json"
  7. )
  8. // StrToBytes 快读的string转byte
  9. func StrToBytes(s string) []byte {
  10. x := (*[2]uintptr)(unsafe.Pointer(&s))
  11. h := [3]uintptr{x[0], x[1], x[1]}
  12. return *(*[]byte)(unsafe.Pointer(&h))
  13. }
  14. func PrintStruct(prefix string, data interface{}) {
  15. bytes, _ := json.Marshal(data)
  16. fmt.Printf("%s, %s\n", prefix, bytes)
  17. }
  18. func VerifyEmailFormat(email string) bool {
  19. pattern := `\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*` //匹配电子邮箱
  20. reg := regexp.MustCompile(pattern)
  21. return reg.MatchString(email)
  22. }
  23. func VerifyMobileFormat(phone string) bool {
  24. 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}$"
  25. reg := regexp.MustCompile(regular)
  26. return reg.MatchString(phone)
  27. }