This question already has an answer here:
I have a package:
package fruit
import "fmt"
type Fruit struct {}
func (f Fruit) Banana(count int) string {
return fmt.Sprintf("%d bananas")
}
func (f Fruit) Melon(count int) string {
return fmt.Sprintf("%d melons")
}
And I want to use reflection to process all the methods on the Fruit type and print out "Banana" and "Melon". I'm having trouble getting the method names. Here's what I have so far:
package main
import (
"fmt"
"reflect"
"runtime"
)
r := reflect.ValueOf(Fruit{})
for i := 0; i < r.NumMethod(); i++ {
method := r.Method(i)
methodName := runtime.FuncForPC(method.Pointer()).Name()
fmt.Println(methodName)
}
Unfortunately, when I run it, I just get the name of some inner mechanism of reflection:
reflect.methodValueCall
reflect.methodValueCall
Aucun commentaire:
Enregistrer un commentaire