package main import "runtime" import "log" func main() { test() } func test() { test2() } func test2(){ pc,file,line,ok := runtime.Caller(2) log.Println(pc) log.Println(file) log.Println(line) log.Println(ok) f := runtime.FuncForPC(pc) log.Println(f.Name()) pc,file,line,ok = runtime.Caller(0) log.Println(pc) log.Println(file) log.Println(line) log.Println(ok) f = runtime.FuncForPC(pc) log.Println(f.Name()) pc,file,line,ok = runtime.Caller(1) log.Println(pc) log.Println(file) log.Println(line) log.Println(ok) f = runtime.FuncForPC(pc) log.Println(f.Name()) }
输出
2011/12/30 22:55:17 8216 2011/12/30 22:55:17 /Users/jarcher/workspace/go/test/src/linetest.go 2011/12/30 22:55:17 7 2011/12/30 22:55:17 true 2011/12/30 22:55:17 main.main 2011/12/30 22:55:17 9065 2011/12/30 22:55:17 /Users/jarcher/workspace/go/test/src/linetest.go 2011/12/30 22:55:17 23 2011/12/30 22:55:17 true 2011/12/30 22:55:17 main.test2 2011/12/30 22:55:17 8241 2011/12/30 22:55:17 /Users/jarcher/workspace/go/test/src/linetest.go 2011/12/30 22:55:17 11 2011/12/30 22:55:17 true 2011/12/30 22:55:17 main.test