go 反射解析结构体标签tag

package main

import (

  "fmt"
    "reflect"
 
)

type resume struct {
    Name string `info:"name" doc:"我的名字"`
    Sex string `info:"Sex"`
}
func findTag(str interface{}) {
    t := reflect.TypeOf(str).Elem()
    
    for i := 0;i<t.NumField();i++{
        tagInfo := t.Field(i).Tag.Get("info")
        tagDoc := t.Field(i).Tag.Get("doc")
        fmt.Println("info:",tagInfo,"doc:",tagDoc)
    }
}
func main() {
re := resume{}
findTag(&re)
}

原文地址:https://www.cnblogs.com/ithubb/p/14553411.html