I currently have the following code
func (r *Router) Get(path string, controller interface{}) {
...
t := reflect.TypeOf(controller)
...
}
That is called doing the following
Route.Get("/", controllers.Test.IsWorking)
The second argument is basically this
type Test struct {
*Controller
Name string
}
func (t Test) IsWorking() {
}
type Controller struct {
Method string
Request *http.Request
Response http.ResponseWriter
Data map[interface{}]interface{}
}
I want to get the struct the function refers to. create a new struct of that type and call the function so for example
controllers.Test.IsWorking
Create a Test struct and call IsWorking()
To make a new pointer to a newly allocated struct of the type in the interface, all you need is
newController := reflect.New(reflect.TypeOf(controller)).Interface()
Or to set a value on the new instance first:
newController := reflect.New(reflect.TypeOf(controller))
newController.Elem().FieldbyName("Method").Set(reflect.ValueOf("GET"))
If you want to create a pointer to a struct and call the IsWorking
method, it looks like
t := reflect.New(reflect.TypeOf(Test{}))
t.MethodByName("IsWorking").Call(nil)