I want to return the name of a struct attribute using the reflect package. So far I have:
type MultiQuestions struct {
QuestionId int64
QuestionType string
QuestionText string
}
func (q *MultiQuestions) StructAttrName() string {
return reflect.ValueOf(q).Elem().Field(0).Name
}
However, this gives me a error reflect.ValueOf(q).Elem().Field(0).Name undefined (type reflect.Value has no field or method Name)
I tried casting to StructField but that didn't work either. How do I get at the name of Struct?
In this case, the names I am interested in are QuestionId, QuestionType and QuestionText.