I used Go's reflection function to get the type of the method, but I found that the method I can get is related to whether the recipient is a pointer. Why is that?
When I ran the code below, I got the following result。
--- value interface ---
Test
--- pointer interface ---
Test
ToString
I don't understand how the result obtained by the Method method is related to whether the recipient is a pointer or not.
type User struct{}
type Admin struct{ User }
func (*User) ToString() {
fmt.Println("tostring")
}
func (Admin) Test() {
fmt.Println("test")
}
func main27() {
var u Admin
methods := func(t reflect.Type) {
for i, n := 0, t.NumMethod(); i < n; i++ {
m := t.Method(i)
fmt.Println(m.Name)
}
}
fmt.Println("--- value interface ---")
methods(reflect.TypeOf(u))
fmt.Println("--- pointer interface ---")
methods(reflect.TypeOf(&u))
}