1

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.

Jordan
  • 3,998
  • 9
  • 45
  • 81
  • 1
    Does this answer your question? [Is it possible to dynamically create a function with a receiver (method) in go?](https://stackoverflow.com/questions/28481354/is-it-possible-to-dynamically-create-a-function-with-a-receiver-method-in-go) – Hymns For Disco Jan 29 '21 at 17:24
  • 1
    No would be the answer. – Volker Jan 29 '21 at 17:39

0 Answers0