代码拉取完成,页面将自动刷新
/*
* @desc: 反射相关工具方法
* @Author: lfzxs@qq.com
* @Date: 2022/5/10 9:28
*/
package libUtils
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
)
/**
* item: 字段项
* value: 项值对象
**/
type ForEachStructCallBakck func (item reflect.StructField,value any)
func FieldInfoCallBack(item reflect.StructField,value any){
field := item
fmt.Printf("%s offset %d anonymous %t type %s exported %t json tag %s\n",
field.Name, //变量名称
field.Offset, //相对于结构体首地址的内存偏移量,string类型会占据16个字节
field.Anonymous, //是否为匿名成员
field.Type, //数据类型,reflect.Type类型
field.IsExported(), //包外是否可见(即是否以大写字母开头)
field.Tag.Get("json")) //获取成员变量后面``里面定义的tag
}
// 遍历结构体成员
func ForEachStruct(model any,callback ForEachStructCallBakck){
typeOf := reflect.TypeOf(model) //需要用struct的Type
//如果是指针
if typeOf.Kind() == reflect.Ptr {
typeOf = typeOf.Elem()
}
valueOf := reflect.ValueOf(model) //成员变量的个数
fieldNum := typeOf.NumField() //成员变量的个数
for i := 0; i < fieldNum; i++ {
field := typeOf.Field(i)
value := valueOf.Field(i).Interface()
callback(field,value)
}
}
// 取结构体指定名称成员的值
func GetStructItemValue(model any,name string)(any,bool){
var result any
typeOf := reflect.TypeOf(model) //需要用struct的Type
valueOf := reflect.ValueOf(model)
_,ok := typeOf.FieldByName(name)
if(ok){
result = valueOf.FieldByName(name).Interface()
}
return result,ok
}
//interface{}转换成字符串
func InterfaceToString(value interface{}) string {
// interface 转 string
var key string
if value == nil {
return key
}
switch value := value.(type) {
case float64:
ft := value
key = strconv.FormatFloat(ft, 'f', -1, 64)
case float32:
ft := value
key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
case int:
it := value
key = strconv.Itoa(it)
case uint:
it := value
key = strconv.Itoa(int(it))
case int8:
it := value
key = strconv.Itoa(int(it))
case uint8:
it := value
key = strconv.Itoa(int(it))
case int16:
it := value
key = strconv.Itoa(int(it))
case uint16:
it := value
key = strconv.Itoa(int(it))
case int32:
it := value
key = strconv.Itoa(int(it))
case uint32:
it := value
key = strconv.Itoa(int(it))
case int64:
it := value
key = strconv.FormatInt(it, 10)
case uint64:
it := value
key = strconv.FormatUint(it, 10)
case string:
key = value
case []byte:
key = string(value)
default:
newValue, _ := json.Marshal(value)
key = string(newValue)
}
return key
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。