调用函数:
func FuncForPC(pc uintptr) *Func {
func Caller(skip int) Frame {
例子:
package main
import (
"fmt"
"runtime"
)
func main () {
funcName, file, line, ok := runtime.Caller(0)
if ok {
fmt.Println("file:", file, " func:", runtime.FuncForPC(funcName).Name(), " line:", line)
}
}
执行结果:
file: /root/use.go func: main.main line: 23

本文介绍了如何在Golang中调用函数获取当前文件名、执行的函数名以及代码所在的行数,通过示例展示了具体实现。
1436

被折叠的 条评论
为什么被折叠?



