NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
在 Go 语言中,reflect 实现了运行时反射。reflect 包会帮助识别 interface{} 变量的底层具体类型和具体值 ### reflect.TypeOf vs reflect.ValueOf * reflect.TypeOf 返回类型(reflect.Type) * reflect.ValueOf 返回值(reflect.Value) * 可以从reflect.Value获得类型 * 通过kind来判断类型 ### NumField()和Field()方法 NumField() :方法返回结构体中字段的数量 Field(i int) 方法返回字段 i 的 reflect.Value ### go kind()类型 ~~~ const ( Invalid Kind = iota Bool Int Int8 Int16 Int32 Int64 Uint Uint8 Uint16 Uint32 Uint64 Uintptr Float32 Float64 Complex64 Complex128 Array Chan Func Interface Map Ptr Slice String Struct UnsafePointer ) ~~~ ### StructField ``` type StructField struct { Name string PkgPath string Type Type // field type Tag StructTag // field tag string Offset uintptr // offset within struct, in bytes Index []int // index sequence for Type.FieldByIndex Anonymous bool // is an embedded field } ```