-
Notifications
You must be signed in to change notification settings - Fork 0
/
converting.go
116 lines (91 loc) · 2.08 KB
/
converting.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Copyright © 2024 Patrick Hermann [email protected]
*/
package base
import (
"encoding/base64"
"fmt"
"log"
"math/rand"
"strconv"
"strings"
"time"
)
func ConvertIntegerToString(inputNumber int) (outputNumber string) {
outputNumber = strconv.FormatInt(int64(inputNumber), 10)
return
}
func ConvertStringToInteger(inputNumber string) (outputNumber int) {
outputNumber, err := strconv.Atoi(inputNumber)
if err != nil {
return
}
return
}
func ConvertStringToBoolean(trueOrFalse string) (boolValue bool) {
boolValue, err := strconv.ParseBool(trueOrFalse)
if err != nil {
log.Fatal(err)
}
return
}
func VerifyIfStringIsBase64(s string) bool {
_, err := base64.StdEncoding.DecodeString(s)
return err == nil
}
func DecodeBase64String(base64Input string) string {
stringOutput, err := base64.StdEncoding.DecodeString(base64Input)
if err != nil {
panic(err)
}
return strings.Trim(string(stringOutput), "\n")
}
func GenerateRandomString(length int) string {
rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, length+2)
rand.Read(b)
return fmt.Sprintf("%x", b)[2 : length+2]
}
func MergeMaps[K comparable, V any](m1 map[K]V, m2 map[K]V) map[K]V {
merged := make(map[K]V)
for key, value := range m1 {
merged[key] = value
}
for key, value := range m2 {
merged[key] = value
}
return merged
}
// REMOVE DUPLICATES FROM ANY SLICE USING GENERICS
func UniqueSlice[T comparable](s []T) []T {
inResult := make(map[T]bool)
var result []T
for _, str := range s {
if _, ok := inResult[str]; !ok {
inResult[str] = true
result = append(result, str)
}
}
return result
}
// GET VALUES FROM STRING POINTER
func GetStringPointerValue(s *string) string {
if s != nil {
return *s
}
return ""
}
// CONVERT STRING TO STRING POINTER
func ConvertStringToPointer(s string) *string {
return &s
}
// CONVERT BOOL TO BOOL POINTER
func ConvertBoolToPointer(b bool) *bool {
return &b
}
// GET RANDOM ELEMENT FROM SLICE
func GetRandomPickFromSlice(slice []string) string {
// GET RANDOM ELEMENT
randomIndex := rand.Intn(len(slice))
return slice[randomIndex]
}