I have a package in Go that contains a series of structs. I want to use some form of reflection to:
- Get each
struct
in the package - For each struct in the package, generate a list of the properties + tags values
- Export that information to some document
Consider the following:
package mypackage
type StructA struct {
PropA string `desc:"Some metadata about the property"`
PropB int `desc:"Some more metadata"`
}
type StructB struct {
PropZ string `desc:"Some metadata about the property"`
PropX float `desc:"Some more metadata"`
}
type StructC struct {
PropY string `desc:"Some metadata about the property"`
PropQ int `desc:"Some more metadata"`
}
func GenerateDocs() {
structSlice := somehow.GetStructsIn("mypackage") // How do I do this part??
for i, typedefinition := range structSlice {
// Reflection to get the property names and tags for each struct
}
}
We don't want to instantiate each struct manually. If StructD
is added to the module tomorrow, the code should pick it up and perform the action without us needing to update any of the logic.
How can I get the type definition for each struct in the package programmatically?