type Teacher struct {
Name string `json:"name"`
Age int `json:"age"`
}
func getTag(i interface{}) string
getTag(teacher.Name) // print name
getTag(teacher.Age) // print age
I wanna roll my function like the code segment, but I can't find a way to achieve this. Any Ideas?
maybe this can help. https://play.golang.org/p/WtqdQAmsbi
x := struct {
Bar int `json:"bar"`
Foo string `json:"foo"`
}{2, "foo"}
v := reflect.ValueOf(x)
for i := 0; i < v.NumField(); i++ {
fmt.Println(v.Type().Field(i).Name)
fmt.Println(v.Type().Field(i).Tag)
}
To fetch the name of the struct field as well as its tag