124 lines
2.9 KiB
Go
124 lines
2.9 KiB
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"net/http"
|
||
|
"os/exec"
|
||
|
"reflect"
|
||
|
)
|
||
|
|
||
|
func PrintSlice(s interface{}) {
|
||
|
rv := reflect.ValueOf(s)
|
||
|
if rv.Kind() != reflect.Slice {
|
||
|
fmt.Println("wrong slice")
|
||
|
}
|
||
|
|
||
|
for i := 0; i < rv.Len(); i++ {
|
||
|
elem := rv.Index(i)
|
||
|
if elem.Kind() == reflect.Struct {
|
||
|
// 元素是结构体,打印其字段和值
|
||
|
fmt.Printf("Element %d:\n", i)
|
||
|
for j := 0; j < elem.NumField(); j++ {
|
||
|
field := elem.Type().Field(j)
|
||
|
value := elem.Field(j)
|
||
|
fmt.Printf(" %s: %v\n", field.Name, value.Interface())
|
||
|
}
|
||
|
} else {
|
||
|
// 元素不是结构体,直接打印
|
||
|
fmt.Printf("%d: %v\n", i, elem.Interface())
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// PrintStruct 打印结构体的字段和值
|
||
|
func PrintStruct(s interface{}) {
|
||
|
rt := reflect.TypeOf(s)
|
||
|
rv := reflect.ValueOf(s)
|
||
|
|
||
|
// 确保传入的是结构体
|
||
|
if rt.Kind() != reflect.Struct {
|
||
|
fmt.Println("传入的不是一个结构体")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 遍历结构体的所有字段
|
||
|
for i := 0; i < rt.NumField(); i++ {
|
||
|
field := rt.Field(i)
|
||
|
value := rv.Field(i)
|
||
|
|
||
|
// 打印字段名和值
|
||
|
fmt.Printf("%s: %v\n", field.Name, value.Interface())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// PrintStructPtr 打印通过指针传递的结构体的字段和值
|
||
|
func PrintStructPtr(ptr interface{}) {
|
||
|
// 确保传入的是一个指针
|
||
|
if reflect.TypeOf(ptr).Kind() != reflect.Ptr {
|
||
|
fmt.Println("传入的不是一个指针")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 通过指针访问结构体
|
||
|
rt := reflect.TypeOf(ptr).Elem() // 获取指针指向的类型
|
||
|
rv := reflect.ValueOf(ptr).Elem() // 获取指针指向的值
|
||
|
|
||
|
// 确保指针指向的是结构体
|
||
|
if rt.Kind() != reflect.Struct {
|
||
|
fmt.Println("指针指向的不是一个结构体")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 遍历结构体的所有字段
|
||
|
for i := 0; i < rt.NumField(); i++ {
|
||
|
field := rt.Field(i)
|
||
|
value := rv.Field(i)
|
||
|
|
||
|
// 打印字段名和值
|
||
|
fmt.Printf("%s: %v\n", field.Name, value.Interface())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func FindMaxIndex(slice []float64) int {
|
||
|
if len(slice) == 0 {
|
||
|
return -1
|
||
|
}
|
||
|
|
||
|
maxIndex := 0
|
||
|
maxValue := slice[0]
|
||
|
|
||
|
for i, v := range slice {
|
||
|
if v > maxValue {
|
||
|
maxValue = v
|
||
|
maxIndex = i
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return maxIndex
|
||
|
}
|
||
|
|
||
|
func RunCommand(command string) error {
|
||
|
cmd := exec.Command("sh", "-c", command)
|
||
|
|
||
|
return cmd.Run()
|
||
|
}
|
||
|
|
||
|
func Cors() gin.HandlerFunc {
|
||
|
return func(c *gin.Context) {
|
||
|
method := c.Request.Method
|
||
|
origin := c.Request.Header.Get("Origin")
|
||
|
if origin != "" {
|
||
|
c.Header("Access-Control-Allow-Origin", "*") // 可将将 * 替换为指定的域名
|
||
|
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
|
||
|
c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
|
||
|
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
|
||
|
c.Header("Access-Control-Allow-Credentials", "true")
|
||
|
}
|
||
|
if method == "OPTIONS" {
|
||
|
c.AbortWithStatus(http.StatusNoContent)
|
||
|
}
|
||
|
c.Next()
|
||
|
}
|
||
|
}
|