-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.go
31 lines (27 loc) · 894 Bytes
/
str.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
package gu
import (
"encoding/base64"
"math/rand"
"strings"
"time"
)
// EncodeBase64 is a simple string to string wrapper for encoding a string to the base64 counterpart
func EncodeBase64(input string) string {
return base64.StdEncoding.EncodeToString([]byte(input))
}
// DecodeBase64 is a simple string to string wrapper for decoding a string from base64
func DecodeBase64(input string) (string, error) {
b, err := base64.StdEncoding.DecodeString(input)
return string(b), err
}
// RandomString simply generate a random string of letters with given length
func RandomString(length int) string {
rand.Seed(time.Now().UnixNano())
charSet := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
result := make([]string, 0, length)
for i := 0; i < length; i++ {
idx := rand.Intn(len(charSet))
result = append(result, string(charSet[idx]))
}
return strings.Join(result, "")
}