-
Notifications
You must be signed in to change notification settings - Fork 197
/
functions.go
67 lines (58 loc) · 1.35 KB
/
functions.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
55
56
57
58
59
60
61
62
63
64
65
66
67
package utils
import (
"crypto/md5"
"encoding/base64"
"math/rand"
"time"
)
func CoalesceErr(args ...error) error {
for i := range args {
if args[i] != nil {
return args[i]
}
}
return nil
}
func RandomString(length int) string {
rand.Seed(time.Now().UnixNano())
const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789"
b := make([]byte, length)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
func RandomHexString(length int) string {
rand.Seed(time.Now().UnixNano())
const letterBytes = "abcdef0123456789"
b := make([]byte, length)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
// The Min function returns the smallest of two integers
// Note: math.Min only works for float64
func Min(a int, b int) int {
if a < b {
return a
}
return b
}
// Remove removes an element from a slice at a given index
func Remove(slice []string, index int) []string {
return append(slice[:index], slice[index+1:]...)
}
// IndexOf returns the index of a given value in a slice, or -1 if not found
func IndexOf[T comparable](slice []T, searchValue T) int {
for i, current := range slice {
if current == searchValue {
return i
}
}
return -1
}
func MD5HashBase64(text string) string {
hash := md5.Sum([]byte(text))
return base64.StdEncoding.EncodeToString(hash[:])
}