1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package util
- import (
- "math/rand"
- "time"
- "unicode/utf8"
- )
- // StrLen utf8 string length
- func StrLen(s string) int {
- return utf8.RuneCountInString(s)
- }
- // StrMax verify string's max lenth
- func StrMax(s string, max int) bool {
- n := utf8.RuneCountInString(s)
- if n <= max {
- return true
- }
- return false
- }
- // StrMin verify string's min lenth
- func StrMin(s string, min int) bool {
- n := utf8.RuneCountInString(s)
- if n >= min {
- return true
- }
- return false
- }
- // StrRange verify string's Range lenth
- func StrRange(s string, min, max int) bool {
- n := utf8.RuneCountInString(s)
- if n <= max && n >= min {
- return true
- }
- return false
- }
- // Random 指定长度的随机字符串(字母或数字)
- func Random(n int) string {
- str := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
- bytes := []byte(str)
- result := []byte{}
- r := rand.New(rand.NewSource(time.Now().UnixNano()))
- n1 := len(bytes)
- for i := 0; i < n; i++ {
- result = append(result, bytes[r.Intn(n1)])
- }
- return string(result)
- }
|