How to get the method name in golang

PHPz
Release: 2023-04-25 10:23:03
Original
1688 people have browsed it

When writing Go language code, sometimes you need to get the name of the current function or method for logging or debugging. This article will introduce how to get the method name in Go language.

Go language provides a reflection mechanism that can obtain type information at runtime. Using the reflection mechanism, you can obtain the type information of the current function or method and thus obtain the method name. The following is a simple example program:

package main

import (
    "fmt"
    "reflect"
    "runtime"
)

func main() {
    foo := Foo{}
    foo.Bar()
}

type Foo struct{}

func (f Foo) Bar() {
    fmt.Println(getFunctionName())
}

func getFunctionName() string {
    pc, _, _, _ := runtime.Caller(1)
    fn := runtime.FuncForPC(pc)
    return reflect.ValueOf(fn.Name()).String()
}
Copy after login

Run the above program, the output result is:

main.(Foo).Bar
Copy after login

As you can see, the getFunctionName() function returns the name of the function or method currently calling it. The runtime.Caller() function and runtime.FuncForPC() function are used here to obtain the PC value and function object of the caller of the current function or method respectively, and then use the reflection mechanism to obtain the name of the function object.

It should be noted that the obtained function name contains the name of the type to which it belongs, as well as the function name itself. Therefore, the obtained function name needs to be processed in the program to extract the method name or function name.

The following is a more complex sample program that demonstrates how to extract the name of a function or method from the function name.

package main

import (
    "fmt"
    "reflect"
    "runtime"
    "strings"
)

func main() {
    foo := Foo{}
    foo.Bar()
}

type Foo struct{}

func (f Foo) Bar() {
    fmt.Println(getMethodName())
}

func getMethodName() string {
    pc, _, _, _ := runtime.Caller(1)
    fn := runtime.FuncForPC(pc)
    fullName := fmt.Sprintf("%s", fn.Name())
    lastDotIndex := strings.LastIndex(fullName, ".")
    return fullName[lastDotIndex+1:]
}
Copy after login

Run the above program, the output result is:

Bar
Copy after login

As you can see, the getMethodName() function returns the name of the method currently calling it. Here, the strings.LastIndex() function is used to obtain the position of the last "." character in the function name, and then the string is intercepted from behind the position to obtain the method name.

It should be noted that the getMethodName() function in the above example program is only suitable for obtaining the method name. If you need to get the function name, you need to write different processing logic.

In short, the Go language itself provides some reflection mechanisms that can easily obtain function or method information. Through the reflection mechanism, we can obtain type information at runtime, and then obtain the name, parameters and other information of functions or methods, which is very helpful for program development, debugging and optimization.

The above is the detailed content of How to get the method name in golang. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!