在 go 中,是否可以检索结构的变量注释?考虑以下结构:
type AType struct {
ID string `xml:"my_id"`
Date string `xml:"creation_ts"`
}
如何使用反射检索 ID 字段的 xml:"my_id"
部分?以下将打印变量的名称、类型和值,但不打印注释。
s := reflect.ValueOf(&aType).Elem()
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {
s.Field(i).
f := s.Field(i)
fmt.Printf("%d: %s %s = %v\n", i,
typeOfT.Field(i).Name, f.Type(), f.Interface())
}
谢谢,
最佳答案
关于go - 使用反射检索结构变量的注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43036010/