Using Go's reflect
package, you can create dynamic struct types using reflect.StructOf():
typ := reflect.StructOf([]reflect.StructField{
{
Name: "Height",
Type: reflect.TypeOf(float64(0)),
Tag: `json:"height"`,
},
{
Name: "Age",
Type: reflect.TypeOf(int(0)),
Tag: `json:"age"`,
},
})
Is there any way to create functions/methods that take this new dynamic type as a receiver? Specifically, I'd like to create a dynamic type using StructOf()
that implements an interface (which has several methods defined) such that reflect.New(typ).Interface().(InterfaceType)
will succeed.