代码拉取完成,页面将自动刷新
package estring
import (
"reflect"
"gitee.com/haodreams/golib/snaker"
)
//GetNameStruct 获取类名
func GetNameStruct(class interface{}, isSnake ...bool) (title string) {
v := reflect.ValueOf(class)
v = reflect.Indirect(v)
t := v.Type()
title = t.Name()
if len(isSnake) > 0 && isSnake[0] {
title = snaker.CamelToSnake(title)
}
return
}
//GetTitlesFromStruct 获取类中的成员变量和变量对应的值
func GetTitlesFromStruct(class interface{}) (titles []string) {
v := reflect.ValueOf(class)
if v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
} else if v.Kind() != reflect.Struct {
return
}
t := v.Type()
for i := 0; i < v.NumField(); i++ {
fieldType := t.Field(i)
name := snaker.CamelToSnake(fieldType.Name)
titles = append(titles, name)
}
return
}
//GetStructValues 获取字段对应的值
func GetStructValues(class interface{}) (values []interface{}) {
v := reflect.ValueOf(class)
if v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
} else if v.Kind() != reflect.Struct {
return
}
values = make([]interface{}, v.NumField())
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
typField := field.Type()
if typField.Kind() == reflect.Ptr {
if field.IsNil() {
values[i] = nil
} else {
field = reflect.Indirect(field)
//field = field.Elem() //2种方法等效
values[i] = field.Interface()
}
} else {
values[i] = field.Interface()
}
}
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。