如何在Go中获取函数的名称?

huangapple go评论125阅读模式
英文:

How to get the name of a function in Go?

问题

给定一个函数,是否可以获取它的名称?例如:

  1. func foo() {
  2. }
  3. func GetFunctionName(i interface{}) string {
  4. // ...
  5. }
  6. func main() {
  7. // 将打印 "name: foo"
  8. fmt.Println("name:", GetFunctionName(foo))
  9. }

我被告知runtime.FuncForPC可以帮助,但我无法理解如何使用它。

英文:

Given a function, is it possible to get its name? Say:

  1. func foo() {
  2. }
  3. func GetFunctionName(i interface{}) string {
  4. // ...
  5. }
  6. func main() {
  7. // Will print "name: foo"
  8. fmt.Println("name:", GetFunctionName(foo))
  9. }

I was told that runtime.FuncForPC would help, but I failed to understand how to use it.

答案1

得分: 257

我找到了一个解决方案:

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. "runtime"
  6. )
  7. func foo() {
  8. }
  9. func GetFunctionName(i interface{}) string {
  10. return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
  11. }
  12. func main() {
  13. // 这将打印出 "name: main.foo"
  14. fmt.Println("name:", GetFunctionName(foo))
  15. }
英文:

I found a solution:

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. "runtime"
  6. )
  7. func foo() {
  8. }
  9. func GetFunctionName(i interface{}) string {
  10. return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
  11. }
  12. func main() {
  13. // This will print "name: main.foo"
  14. fmt.Println("name:", GetFunctionName(foo))
  15. }

答案2

得分: 15

不完全是你想要的,因为它记录了文件名和行号,但是这是我在我的Tideland Common Go Library(http://tideland-cgl.googlecode.com/)中使用“runtime”包的方法:

  1. // Debug打印带有文件和行号的调试信息到日志中。
  2. func Debug(format string, a ...interface{}) {
  3. _, file, line, _ := runtime.Caller(1)
  4. info := fmt.Sprintf(format, a...)
  5. log.Printf("[cgl] debug %s:%d %v", file, line, info)
  6. }
英文:

Not exactly what you want, because it logs the filename and the line number, but here is how I do it in my Tideland Common Go Library (http://tideland-cgl.googlecode.com/) using the "runtime" package:

  1. // Debug prints a debug information to the log with file and line.
  2. func Debug(format string, a ...interface{}) {
  3. _, file, line, _ := runtime.Caller(1)
  4. info := fmt.Sprintf(format, a...)
  5. log.Printf("[cgl] debug %s:%d %v", file, line, info)

答案3

得分: 4

我找到了一个更好的解决方案,在下面的这个函数中,你只需要传递一个函数,输出就会变得简单明了。

  1. package main
  2. import (
  3. "reflect";
  4. "runtime";
  5. "strings";
  6. )
  7. func GetFunctionName(temp interface{}) string {
  8. strs := strings.Split((runtime.FuncForPC(reflect.ValueOf(temp).Pointer()).Name()), ".");
  9. return strs[len(strs)-1]
  10. }

这是一个使用示例:

  1. package main
  2. import "fmt";
  3. func main() {
  4. fmt.Println(GetFunctionName(main))
  5. }

你应该期望得到的答案是:

  1. main
英文:

I found a better solution, in this function down here you just simply pass a function and the output is gonna be simple and straight.

  1. package main
  2. import (
  3. "reflect"
  4. "runtime"
  5. "strings"
  6. )
  7. func GetFunctionName(temp interface{}) string {
  8. strs := strings.Split((runtime.FuncForPC(reflect.ValueOf(temp).Pointer()).Name()), ".")
  9. return strs[len(strs)-1]
  10. }

And this is an example of how you use this:

  1. package main
  2. import "fmt"
  3. func main() {
  4. fmt.Println(GetFunctionName(main))
  5. }

And this is the answer you should expect:

  1. main

答案4

得分: 3

通过获取上一个调用者函数名

  1. import (
  2. "os"
  3. "runtime"
  4. )
  5. func currentFunction() string {
  6. counter, _, _, success := runtime.Caller(1)
  7. if !success {
  8. println("functionName: runtime.Caller: failed")
  9. os.Exit(1)
  10. }
  11. return runtime.FuncForPC(counter).Name()
  12. }
英文:

By getting the function name of the previous caller:

  1. import (
  2. "os"
  3. "runtime"
  4. )
  5. func currentFunction() string {
  6. counter, _, _, success := runtime.Caller(1)
  7. if !success {
  8. println("functionName: runtime.Caller: failed")
  9. os.Exit(1)
  10. }
  11. return runtime.FuncForPC(counter).Name()
  12. }

huangapple
  • 本文由 发表于 2011年8月14日 03:12:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/7052693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定