这是我用于解析 xml 的代码。在函数的末尾,我应该在值切片中具有结构的字段值。
func FindAttrs(attrs []Tag, errorChan chan<- error) {
var tableFields []reflect.StructField
for _, v := range attrs {
tableFields = append(tableFields, reflect.StructField{
Name: strings.Title(v.Name),
Type: reflect.TypeOf(""),
Tag: reflect.StructTag(fmt.Sprintf(`xml:"%v,attr"`, v.Name)),
Offset: 0,
PkgPath: "utility",
Index: nil,
Anonymous: false,
})
}
unmarshalStruct := reflect.Zero(reflect.StructOf(tableFields))
err := xml.Unmarshal(ReadBytes(errorChan), &unmarshalStruct)
HandleError(err, "Error parse config", false, errorChan)
values := make([]interface{}, unmarshalStruct.NumField())
for i := 0; i < unmarshalStruct.NumField(); i++ {
values[i] = unmarshalStruct.Field(0).Interface()
}
}
但是,它会惊慌失措地显示以下消息:
reflect.Value.Interface: cannot return value obtained from unexported field or method
我称之为:
utility.FindAttrs([]utility.Tag{
{"name", reflect.String}, {"isUsed", reflect.String},
}, errorChan)
我的xml是<configuration name="mur" isUsed="mur"/>
慕少森
相关分类