/
helpers.go
47 lines (41 loc) · 888 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
package celeritas
import (
"os"
"crypto/rand"
)
const (
randomString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321_+"
)
//RandomString Generate random string
func (c *Celeritas) 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 (c *Celeritas) CreateDirIfNotExist(path string) error {
const mode = 0775
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.Mkdir(path, mode)
if err != nil {
return err
}
}
return nil
}
func (c *Celeritas) 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
}