reflectKit

package
v2.8.125 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ValueOf func(i any) reflect.Value = reflect.ValueOf

ValueOf

@param i 可以为nil

e.g.

v := reflectKit.ValueOf(111)
fmt.Println(v.Kind() == reflect.Int)	// true

e.g.1

{
	var obj interface{} = nil
	v := reflectKit.ValueOf(obj)
	k := v.Kind()
	fmt.Println(k)                    // invalid
	fmt.Println(k == reflect.Invalid) // true
}

{
	type bean struct {
	}

	var obj *bean = nil
	v := reflectKit.ValueOf(obj)
	k := v.Kind()
	fmt.Println(k)                    // ptr
	fmt.Println(k == reflect.Invalid) // false
}

Functions

func GetField

func GetField(ptr interface{}, fieldName string) reflect.Value

GetField

PS: 这样获取到的值是不可以修改的,如果要修改name字段,需要用到reflect.NewAt函数,这个函数通过一个类型值的底层地址(指针 p)和类型, 返回指向该值的一个指针,这个返回值是可寻址的,即可通过它直接访问该值。具体的修改是使用reflect.Value.Set函数。

@param fieldName 字段名(属性名),private或public的都可以

func GetNestedField

func GetNestedField(ptr interface{}, fieldNames ...string) (reflect.Value, error)

GetNestedField 获取(层层嵌套的)字段

func KindOf added in v2.8.115

func KindOf(i any) reflect.Kind

func SetField

func SetField(ptr interface{}, fieldName string, newFieldValue interface{}) error

SetField

@param fieldName 字段名(属性名),private或public的都可以 @param newFieldValue 字段的新值

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL