July 2019
Intermediate to advanced
458 pages
12h 12m
English
A structure field carries plenty of information, from the field name and index to its tag:
type StructField struct { Name string PkgPath string Type Type // field type Tag StructTag // field tag string Offset uintptr // offset within struct, in bytes Index []int // index sequence for Type.FieldByIndex Anonymous bool // is an embedded field}
A reflect.StructField value can be obtained using the reflect.Type methods:
They are the same methods used by reflect.Value, but they return different types. The NumField method returns the total number of fields for the structure, allowing us to execute an iteration:
type Person struct { Name string `json:"name,omitempty" xml:"-"` Surname string `json:"surname,omitempty" ...Read now
Unlock full access