I have a field struct type:
{
Name: "fieldA",
Type: "string",
}
and an array of this filed type:
[{
Name: "fieldA"
Type: "string"
},
{
Name: "filedB",
Type: "int",
}
...
This array may change or grow later.
Now I want to define a new struct type based on this array in runtime, like this:
type myStruct struct {
fieldA string,
fieldB int,
...
}
I think using reflection, I can get a myStruct instance by calling reflect.StructOf() but can I get the type? Is this possible?
Thanks
It seems that there is a misunderstanding here.
reflect.StructOf()returns the struct type containing fields. It does not return "a myStruct instance". It looks like this is already the type you want to get, right?This demo from the official document for reflect.StructOf should make it easy to understand the concepts:
Output: