0PricingLogin
Go Academy · Lesson

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

  1. reflect.Type and reflect.Value
  2. Inspecting Struct Fields and Tags
  3. Dynamic Function Calls
  4. Practical Reflection: Serializers
← Back to Go Academy