How Can I Retrieve a Function's Name Using Reflection in Go?

DDD
Release: 2024-11-09 19:44:02
Original
593 people have browsed it

How Can I Retrieve a Function's Name Using Reflection in Go?

Retrieving Function Name via Reflection

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)
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template