isayme / blog

https://blog.isayme.org
MIT License
10 stars 2 forks source link

Golang 获取当前运行函数名 #14

Open isayme opened 6 years ago

isayme commented 6 years ago

代码

func getCurrentFuncName() string {
    pc := make([]uintptr, 1)
    runtime.Callers(2, pc)
    f := runtime.FuncForPC(pc[0])
    return f.Name()
}

还有另外一种写法, 但是效率低了很多

详细见: https://github.com/isayme/blog/blob/master/issues/14/

执行make bench 查看 benchmark 数据

参考资料