Skip to content

Commit

Permalink
Update v0.0.92
Browse files Browse the repository at this point in the history
  • Loading branch information
george012 committed Aug 26, 2023
1 parent 5c052bc commit 18605bb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ package config

const (
ProjectName = "gtbox"
ProjectVersion = "v0.0.91"
ProjectVersion = "v0.0.92"
ProjectDescription = "[Golang]日常开发工具箱"
)
31 changes: 31 additions & 0 deletions gtbox_reflect/gtbox_reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Package gtbox_reflect 反射方法工具库
package gtbox_reflect

import (
"fmt"
"reflect"
)

Expand All @@ -20,3 +21,33 @@ func GetNumberOfFieldWithModel(customModel interface{}) int {
return 0
}
}

// GetFieldNameAtIndex 获取指定索引位置的--字段名
func GetFieldNameAtIndex(customModel interface{}, index int) (string, error) {
typ := reflect.TypeOf(customModel)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
if typ.Kind() == reflect.Struct {
if index >= 0 && index < typ.NumField() {
return typ.Field(index).Name, nil
}
return "", fmt.Errorf("Index out of range")
}
return "", fmt.Errorf("Provided interface is not a struct or pointer to struct")
}

// GetFieldValueAtIndex 获取指定索引位置的字段--Value值
func GetFieldValueAtIndex(customModel interface{}, index int) (interface{}, error) {
val := reflect.ValueOf(customModel)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() == reflect.Struct {
if index >= 0 && index < val.NumField() {
return val.Field(index).Interface(), nil
}
return nil, fmt.Errorf("Index Out Of Range")
}
return nil, fmt.Errorf("Provided interface is not a struct or pointer to struct")
}

0 comments on commit 18605bb

Please sign in to comment.