-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
46 lines (36 loc) · 971 Bytes
/
string.go
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
37
38
39
40
41
42
43
44
45
46
package chance
// StringOption is a type
type StringOption func(*StringOptions)
// StringOptions is string options
type StringOptions struct {
length int
pool string
}
func (ch *chance) String(options ...StringOption) string {
ops := StringOptions{length: 5, pool: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()[]"}
for i := range options {
options[i](&ops)
}
ch.r.Seed(ch.seed)
str := ""
for i := 0; i < ops.length; i++ {
str += string(ops.pool[ch.r.Intn(len(ops.pool))])
}
return str
}
// String returns a random string
func String(options ...StringOption) string {
return defaultChance.String(options...)
}
// SetStringLength sets length of random string
func SetStringLength(length int) StringOption {
return func(sch *StringOptions) {
sch.length = length
}
}
// SetStringPool sets pool of random string
func SetStringPool(pool string) StringOption {
return func(sch *StringOptions) {
sch.pool = pool
}
}