add helper package
This commit is contained in:
parent
68ede537b5
commit
ed3f0eef79
3 changed files with 44 additions and 0 deletions
16
helper/anyToString.go
Normal file
16
helper/anyToString.go
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
package helper
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func AnyToString(value interface{}) string {
|
||||||
|
switch v := value.(type) {
|
||||||
|
case string:
|
||||||
|
return v
|
||||||
|
case float64:
|
||||||
|
return fmt.Sprintf("%.2f", v)
|
||||||
|
case int:
|
||||||
|
return fmt.Sprintf("%d", v)
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
15
helper/generaterandomString.go
Normal file
15
helper/generaterandomString.go
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
package helper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenerateRandomString(length int) string {
|
||||||
|
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||||
|
// Generate random string
|
||||||
|
result := make([]byte, length)
|
||||||
|
for i := range result {
|
||||||
|
result[i] = charset[rand.Intn(len(charset))]
|
||||||
|
}
|
||||||
|
return string(result)
|
||||||
|
}
|
||||||
13
helper/sortMapKeys.go
Normal file
13
helper/sortMapKeys.go
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
package helper
|
||||||
|
|
||||||
|
import "sort"
|
||||||
|
|
||||||
|
// Helper function to get sorted keys from a map
|
||||||
|
func SortMapKeys(data map[string]interface{}) []string {
|
||||||
|
keys := make([]string, 0, len(data))
|
||||||
|
for key := range data {
|
||||||
|
keys = append(keys, key)
|
||||||
|
}
|
||||||
|
sort.Strings(keys) // Sort keys alphabetically
|
||||||
|
return keys
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue