I was trying to generalize my code by using reflection to call all methods of a type. It's easy and straightforward but there is a problem with that, reflection.TypeOf(T).NumMethods (or other methods) ignores methods which used receiver type as a pointer. For example this small code will print 1 instead of 2:
package main
import (
"fmt"
"reflect"
)
type Foo struct {}
func (f Foo) Bar() {}
func (f *Foo) Baz() {}
func main() {
obj := Foo{}
fmt.Println(reflect.TypeOf(obj).NumMethod())
}
You can run in playground. It prints 1 because of Bar method. If you delete the pointer (*) from Baz, it will print 2.
My question is how can I list all methods regardless of receiver type.
Thanks