Inspecting Struct Fields and Tags
Iterating fields and reading struct tags
StructField type
reflect.StructField describes one field of a struct: Name, Type, Tag, Index, Offset, Anonymous, and IsExported.
Iterating fields
Loop over all fields of a struct type:
t := reflect.TypeOf(struct{ ID int; Name string }{})
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
fmt.Println(f.Name, f.Type, f.IsExported())
}All lessons in this course
- reflect.Type and reflect.Value
- Inspecting Struct Fields and Tags
- Dynamic Function Calls
- Practical Reflection: Serializers