大话西游666
您可以runtime.Caller用于轻松检索有关呼叫者的信息:func Caller(skip int) (pc uintptr, file string, line int, ok bool)示例 #1:打印调用者文件名和行号:https : //play.golang.org/p/cdO4Z4ApHSpackage mainimport ( "fmt" "runtime")func foo() { _, file, no, ok := runtime.Caller(1) if ok { fmt.Printf("called from %s#%d\n", file, no) }}func main() { foo()}示例 #2:获取更多信息runtime.FuncForPC:https : //play.golang.org/p/y8mpQq2mAvpackage mainimport ( "fmt" "runtime")func foo() { pc, _, _, ok := runtime.Caller(1) details := runtime.FuncForPC(pc) if ok && details != nil { fmt.Printf("called from %s\n", details.Name()) }}func main() { foo()}