我正在尝试通过反射访问接口中的数组。
在其他字段中,我还有一个字符串数组:
type Configuration struct { ... SysVars []string}
我可以像这样访问SysVars字段:
elem := reflect.ValueOf(conf).Elem() sysVarsInterface := elem.FieldByName("SysVars").Interface()
此时,在使用 GoLand 的调试器时,我可以看到sysVarsInterface是一个具有我期望的两个值的接口。由于它是一个数组,我假设我需要将它视为接口并再次反映它?所以它看起来像这样:
sysVarsValue := reflect.ValueOf(&sysVarsInterface) sysVarsElem := sysVarsValue.Elem()
但迭代失败:
for i:=0; i< sysVarsElem.NumField(); i++ { vname := sysVarsElem.Type().Field(i).Name fmt.Println(vname) }
说:
panic: reflect: call of reflect.Value.NumField on interface Value
任何想法我做错了什么?
largeQ
相关分类