I'm having a few problems iterating through *T funcs
from a struct
using reflect
.
I've searched a lot of answers but none seems to talk specifically about this situation.
I've found a reflect.NewAt
at golang documentation but to be honest I didn't understand, and again I couldn't find a single answer for my situation.
For a better understanding... by having the following struct:
type Counter struct {}
func (self *Counter) Add(n int) {}
If I use reflect by calling the struct pointer itself, it works as expected:
y := reflect.TypeOf(&Counter{})
for k := 0; k < y.NumMethod(); k++ {
fmt.Println(y.Method(k)) // {Add func(*Counter, int) <func(*Counter, int) Value> 0}
}
But in my case, multiple structs can arrive here, so it arrives as an interface:
var p interface{} = Counter{}
z := reflect.New(reflect.TypeOf(p))
for k := 0; k < z.NumMethod(); k++ {
fmt.Println(z.Method(k)) // 0x47d150
}
But as shown, it prints the memory address.
I expect the 0x47d150
to be the same output as I was using the pointer directly. What I'm doing wrong here?