-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.go
More file actions
36 lines (31 loc) · 932 Bytes
/
string.go
File metadata and controls
36 lines (31 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Package lol
* @Author iFurySt <ifuryst@gmail.com>
* @Date 2024/6/18
*/
package lol
import (
"math/rand"
"time"
)
const defCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// RandomString generates a random string with a given length.
// If a charset is provided, it will be used to generate the random string.
// Otherwise, the default charset will be used.
//
// Play: https://go.dev/play/p/jpdJyEVdLcq
func RandomString(length int, charset ...string) string {
if len(charset) > 0 && len(charset[0]) > 0 {
return randomString(length, charset[0])
}
return randomString(length, defCharset)
}
// randomString generates a random string with a given length and charset.
func randomString(length int, charset string) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, length)
for i := range b {
b[i] = charset[r.Intn(len(charset))]
}
return string(b)
}