68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"reflect"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
//@author: [piexlmax](https://github.com/piexlmax)
|
||
|
//@function: StructToMap
|
||
|
//@description: 利用反射将结构体转化为map
|
||
|
//@param: obj interface{}
|
||
|
//@return: map[string]interface{}
|
||
|
|
||
|
func StructToMap(obj interface{}) map[string]interface{} {
|
||
|
obj1 := reflect.TypeOf(obj)
|
||
|
obj2 := reflect.ValueOf(obj)
|
||
|
|
||
|
data := make(map[string]interface{})
|
||
|
for i := 0; i < obj1.NumField(); i++ {
|
||
|
if obj1.Field(i).Tag.Get("mapstructure") != "" {
|
||
|
data[obj1.Field(i).Tag.Get("mapstructure")] = obj2.Field(i).Interface()
|
||
|
} else {
|
||
|
data[obj1.Field(i).Name] = obj2.Field(i).Interface()
|
||
|
}
|
||
|
}
|
||
|
return data
|
||
|
}
|
||
|
|
||
|
//@author: [piexlmax](https://github.com/piexlmax)
|
||
|
//@function: ArrayToString
|
||
|
//@description: 将数组格式化为字符串
|
||
|
//@param: array []interface{}
|
||
|
//@return: string
|
||
|
|
||
|
func ArrayToString(array []interface{}) string {
|
||
|
return strings.Replace(strings.Trim(fmt.Sprint(array), "[]"), " ", ",", -1)
|
||
|
}
|
||
|
|
||
|
func Pointer[T any](in T) (out *T) {
|
||
|
return &in
|
||
|
}
|
||
|
|
||
|
func FirstUpper(s string) string {
|
||
|
if s == "" {
|
||
|
return ""
|
||
|
}
|
||
|
return strings.ToUpper(s[:1]) + s[1:]
|
||
|
}
|
||
|
|
||
|
func FirstLower(s string) string {
|
||
|
if s == "" {
|
||
|
return ""
|
||
|
}
|
||
|
return strings.ToLower(s[:1]) + s[1:]
|
||
|
}
|
||
|
|
||
|
// MaheHump 将字符串转换为驼峰命名
|
||
|
func MaheHump(s string) string {
|
||
|
words := strings.Split(s, "-")
|
||
|
|
||
|
for i := 1; i < len(words); i++ {
|
||
|
words[i] = strings.Title(words[i])
|
||
|
}
|
||
|
|
||
|
return strings.Join(words, "")
|
||
|
}
|