-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
49 lines (41 loc) · 954 Bytes
/
helpers.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
package rasant
import (
"crypto/rand"
"os"
)
const (
randomString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321_+"
)
// RandomString generates a random string length n from values in the const randomString
func (ras *Rasant) RandomString(n int) string {
s, r := make([]rune, n), []rune(randomString)
for i := range s {
p, _ := rand.Prime(rand.Reader, len(r))
x, y := p.Uint64(), uint64(len(r))
s[i] = r[x%y]
}
return string(s)
}
func (ras *Rasant) CreateDirIfNotExist(path string) error {
const mode = 0755
if _, err := os.Stat(path); os.IsNotExist(err) {
err := os.Mkdir(path, mode)
if err!= nil {
return err
}
}
return nil
}
func (ras *Rasant) CreateFileIfNotExist(path string) error {
var _, err = os.Stat(path)
if os.IsNotExist(err) {
var file, err = os.Create(path)
if err!= nil {
return err
}
defer func(file os.File) {
_ = file.Close()
}(*file)
}
return nil
}