作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
例如,我想打印出某个结构的每个函数的函数名。
除了我在每个成员函数的开头使用fmt.Println
,还有什么更好的方法吗?
最佳答案
package main
import "fmt"
import "runtime"
func main() {
pc, _, _, _ := runtime.Caller(0)
fmt.Println("Name of function: " + runtime.FuncForPC(pc).Name())
fmt.Println()
// or, define a function for it
fmt.Println("Name of function: " + funcName())
x()
}
func funcName() string {
pc, _, _, _ := runtime.Caller(1)
return runtime.FuncForPC(pc).Name()
}
func x() {
fmt.Println("Name of function: " + funcName())
y()
}
func y() {
fmt.Println("Name of function: " + funcName())
z()
}
func z() {
fmt.Println("Name of function: " + funcName())
}
Output:
Name of function: main.main
Name of function: main.main
Name of function: main.x
Name of function: main.y
Name of function: main.z
(从 here 复制)
关于戈朗 : How to run the same logic at the beginning of every struct member functions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34848961/
我是一名优秀的程序员,十分优秀!