Reflecting on a function's type does not yield its name. Instead, employ the FuncForPc utility to retrieve a *Func instance. This instance provides access to the function's name, formatted as package-qualified, e.g., "main.main". If the unqualified name is desired, simply tokenize the result.
import ( "fmt" "reflect" "runtime" ) func main() { name := runtime.FuncForPC(reflect.ValueOf(main).Pointer()).Name() fmt.Println("Name of function:", name) }
The above is the detailed content of How Can I Retrieve a Function's Name Using Reflection in Go?. For more information, please follow other related articles on the PHP Chinese website!