-
Notifications
You must be signed in to change notification settings - Fork 1
/
random.go
54 lines (44 loc) · 1.11 KB
/
random.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
48
49
50
51
52
53
54
package xstr
import (
"math/rand"
)
// TODO: Add some string random generation function
// Random generate string
// https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go
func Random(n int) string {
return ""
}
// Letters a-Z
const Letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// Numbers 0-9
const Numbers = "0123456789"
// LettersNumbers a-z0-9
const LettersNumbers = Letters + Numbers
func randomStr(n, t int) string {
b := make([]byte, n)
for i := range b {
switch t {
case 0:
b[i] = Letters[rand.Intn(len(Letters))]
case 1:
b[i] = Numbers[rand.Intn(len(Numbers))]
case 2:
b[i] = LettersNumbers[rand.Intn(len(LettersNumbers))]
default:
b[i] = Letters[rand.Intn(len(Letters))]
}
}
return string(b)
}
// RandomCharStr random-string only with letters
func RandomCharStr(n int) string {
return randomStr(n, 0)
}
// RandomNumStr random-string only with numbers
func RandomNumStr(n int) string {
return randomStr(n, 1)
}
// RandomStr random-string with letters and numbers
func RandomStr(n int) string {
return randomStr(n, 2)
}