func Caller(skip int) (pc uintptr, file string, line int, ok bool)
Caller reports file and line number information about function invocations on the calling goroutine’s stack. The argument skip is the number of stack frames to ascend, with 0 identifying the caller of Caller. (For historical reasons the meaning of skip differs between Caller and Callers.) The return values report the program counter, file name, and line number within the file of the corresponding call. The boolean ok is false if it was not possible to recover the information.
参数skip代表要追溯到堆栈的级数
[source lang=”c”]
package main
import (
"fmt"
"runtime"
)
func main() {
hello()
}
func hello() {
pc, file, line, ok := runtime.Caller(0);
if !ok {
return
}
// get Fun object from pc
fun := runtime.FuncForPC(pc)
funcNmae := fun.Name()
fmt.Printf("[%s:%d] Func name %s\n", file, line, funcNmae)
pc, file, line, ok = runtime.Caller(1)
if !ok {
return
}
// get Fun object from pc
fun = runtime.FuncForPC(pc)
funcNmae = fun.Name()
fmt.Printf("[%s:%d] Func name %s\n", file, line, funcNmae)
}
[/source]