mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-04-17 04:55:46 +00:00
18 lines
543 B
Go
18 lines
543 B
Go
|
|
package utils
|
||
|
|
|
||
|
|
import (
|
||
|
|
"reflect"
|
||
|
|
"unsafe"
|
||
|
|
)
|
||
|
|
|
||
|
|
// AccessField can used to access unexported field of a struct
|
||
|
|
// valueType must be the exact type of the field or it will panic
|
||
|
|
func AccessField[valueType any](obj any, fieldName string) *valueType {
|
||
|
|
field := reflect.ValueOf(obj).Elem().FieldByName(fieldName)
|
||
|
|
if field.Type() != reflect.TypeOf(*new(valueType)) {
|
||
|
|
panic("field type: " + field.Type().String() + ", valueType: " + reflect.TypeOf(*new(valueType)).String())
|
||
|
|
}
|
||
|
|
v := (*valueType)(unsafe.Pointer(field.UnsafeAddr()))
|
||
|
|
return v
|
||
|
|
}
|