reflect.Value 使用检索字段后,Field(i)可以调用Interface(). 接口值代表字段值。* }0 B% D9 V) @% Y0 p
没有函数可以将字段值转换为特定类型,因为你可能知道,go 中没有泛型。因此,与签名没有任何功能。GetValue() T 与T字段的类型(当然,这取决于字段)。 " M: d+ g: R H6 L3 x
您可以在 go 中最接近的是GetValue() interface{},这就是所在reflect.Value.Interface() 提供。 ; p$ u4 d" ]9 D7 G+ a
如何使用反射 ( )play ) 在结构中获得每个导出字段的值:[code]import "fmt" "reflect")func main() x := struct{Foo string; Bar int }{"foo",2} v := reflect.ValueOf(x) values := make([]interface{},v.NumField() for i := 0; i < v.NumField(); i values = v.Field(i).Interface() fmt.Println(values)}8 I" z5 k& i) H& [6 T