【发布时间】:2017-03-26 23:58:27
【问题描述】:
在 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())
}
谢谢,
【问题讨论】:
-
这个答案的例子是:What are the use(s) for tags in Go?
标签: go