-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.go
47 lines (38 loc) · 1.06 KB
/
sample.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
47
// File for the generation of random objects, suitable for testing needs
package accord
import (
"fmt"
"math/rand"
"time"
)
const letters = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const digits = "0123456789"
const charset = letters + digits
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()),
)
func RandStringWithCharset(length int, charset string) string {
// for English letters, we can just use bytes instead
// of runes.
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
func RandString(length int) string {
return RandStringWithCharset(length, charset)
}
func GetRandUsername() string {
return fmt.Sprintf("user_%s%s", RandStringWithCharset(3, letters), RandStringWithCharset(3, digits))
}
func GetRandPassword() string {
return RandString(10)
}
func GetRandChannelName() string {
return fmt.Sprintf("chan_%s%s", RandStringWithCharset(3, letters), RandStringWithCharset(3, digits))
}
func GetRandBool() bool {
return seededRand.Intn(2) == 1
}