I have been trying to use the Reflect package to determine the names of the fields in a struct using this method:
func main() {
book := Book{}
e := reflect.ValueOf(&book).Elem()
for i := 0; i < e.NumField(); i++ {
varName := e.Type().Field(i).Name
varType := e.Type().Field(i).Type
varValue := e.Field(i).Interface()
fmt.Printf("%v %v %v\n", varName,varType,varValue)
}
}
according to every example and tutorial I can find. I even found the 'structs' package which exposes a simple struct.Names method which achieves the same result, however all my field names come back like
'ID invalid false'
instead of just
'ID'
I can't find any mention of this anywhere. Thanks for any help.
Aucun commentaire:
Enregistrer un commentaire