用法
type Box struct {
Width int `info:"width" desc:"盒子宽度"`
Height int `info:"height" desc:"盒子高度"`
}
通过反射获取
package main
import (
"fmt"
"reflect"
)
type Box struct {
Width int `info:"width" desc:"盒子宽度"`
Height int `info:"height" desc:"盒子高度"`
}
// 遍历结构体字段
func do(obj interface{}) {
elem := reflect.TypeOf(obj).Elem()
for i:=0;i<elem.NumField();i++{
field := elem.Field(i)
fmt.Printf("Field:%v Desc:%v\n",field.Tag.Get("info"),field.Tag.Get("desc"))
}
}
func main() {
box :=Box{
Width: 10,
Height: 20,
}
do(&box)
}
Field:width Desc:盒子宽度
Field:height Desc:盒子高度