Skip to content

Instantly share code, notes, and snippets.

@songfei1983
Created November 23, 2019 14:13
Show Gist options
  • Save songfei1983/e1661016fb8948b2d08d1f633a038544 to your computer and use it in GitHub Desktop.
Save songfei1983/e1661016fb8948b2d08d1f633a038544 to your computer and use it in GitHub Desktop.
var (
re = regexp.MustCompile(`^(\S.+)\.(\S.+)$`)
)
type CallerInfo struct {
PackageName string
FunctionName string
FileName string
FileLine int
}
func Dump() (callerInfo []*CallerInfo) {
for i := 1; ; i++ {
pc, _, _, ok := runtime.Caller(i) // https://golang.org/pkg/runtime/#Caller
if !ok {
break
}
fn := runtime.FuncForPC(pc)
fileName, fileLine := fn.FileLine(pc)
_fn := re.FindStringSubmatch(fn.Name())
callerInfo = append(callerInfo, &CallerInfo{
PackageName: _fn[1],
FunctionName: _fn[2],
FileName: fileName,
FileLine: fileLine,
})
}
for i := len(callerInfo) - 1; i > -1; i-- {
v := callerInfo[i]
fmt.Printf("%02d: %s.%s@%s:%d\n", i, v.PackageName, v.FunctionName, v.FileName, v.FileLine)
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment