mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-03 15:56:22 +00:00
28 lines
697 B
Go
28 lines
697 B
Go
package utils
|
|
|
|
import "math/rand"
|
|
|
|
type stringsUtils struct{}
|
|
|
|
var Strings = stringsUtils{}
|
|
|
|
func (s *stringsUtils) GenerateRandomString(length int) string {
|
|
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
result := make([]byte, length)
|
|
for i := range result {
|
|
result[i] = charset[rand.Intn(len(charset))]
|
|
}
|
|
return string(result)
|
|
}
|
|
|
|
func (s *stringsUtils) GenerateRandomStringWithCharset(length int, charset string) string {
|
|
if charset == "" {
|
|
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
}
|
|
result := make([]byte, length)
|
|
for i := range result {
|
|
result[i] = charset[rand.Intn(len(charset))]
|
|
}
|
|
return string(result)
|
|
}
|