feat: added modules

This commit is contained in:
Marvin Zhang
2024-06-14 15:42:50 +08:00
parent f1833fed21
commit 0b67fd9ece
626 changed files with 60104 additions and 0 deletions

46
core/utils/array.go Normal file
View File

@@ -0,0 +1,46 @@
package utils
import (
"errors"
"math/rand"
"reflect"
"time"
)
func StringArrayContains(arr []string, str string) bool {
for _, s := range arr {
if s == str {
return true
}
}
return false
}
func GetArrayItems(array interface{}) (res []interface{}, err error) {
switch reflect.TypeOf(array).Kind() {
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
obj, ok := s.Index(i).Interface().(interface{})
if !ok {
return nil, errors.New("invalid type")
}
res = append(res, obj)
}
default:
return nil, errors.New("invalid type")
}
return res, nil
}
func ShuffleArray(slice []interface{}) (err error) {
r := rand.New(rand.NewSource(time.Now().Unix()))
for len(slice) > 0 {
n := len(slice)
randIndex := r.Intn(n)
slice[n-1], slice[randIndex] = slice[randIndex], slice[n-1]
slice = slice[:n-1]
}
return nil
}