liujiandao
2024-04-25 c0f8f8d3a74dbdab4f6ab4926fc664d818fb50f2
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
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)
}