-
Notifications
You must be signed in to change notification settings - Fork 53
/
string-utils.go
54 lines (46 loc) · 1.16 KB
/
string-utils.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 utils
import (
"crypto/rand"
"encoding/base64"
"strconv"
"github.com/google/uuid"
"github.com/segmentio/ksuid"
)
const (
errorRandomString string = "SomethingRandomWentWrong"
)
// GenRandomString - Helper to generate a random string of n characters
func GenRandomString(n int) string {
b := make([]byte, n)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return errorRandomString
}
return base64.URLEncoding.EncodeToString(b)[:n]
}
// GenKSUID - Helper to generate a KSUID
// See https://github.com/segmentio/ksuid for more info about KSUIDs
func GenKSUID() string {
return ksuid.New().String()
}
// GenUUID - Helper to generate a UUID
// See https://github.com/google/uuid for more info about UUIDs
func GenUUID() string {
return uuid.New().String()
}
// StringToInteger - Helper to convert a string into integer
func StringToInteger(s string) int64 {
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0
}
return v
}
// StringToBoolean - Helper to convert a string into boolean
func StringToBoolean(s string) bool {
if s == "yes" || s == "true" || s == "1" {
return true
}
return false
}