Skip to content

Instantly share code, notes, and snippets.

@peacefixation
Created April 10, 2019 03:05
Show Gist options
  • Save peacefixation/fc40b028476fac2aa1e396443688fd1d to your computer and use it in GitHub Desktop.
Save peacefixation/fc40b028476fac2aa1e396443688fd1d to your computer and use it in GitHub Desktop.
Trace function name, line number
func trace() string {
pc := make([]uintptr, 15)
n := runtime.Callers(2, pc) // 2 -> the calling function's frame details
frames := runtime.CallersFrames(pc[:n])
if frame, ok := frames.Next(); ok {
// frame.File is the full file path
return fmt.Sprintf("%s:%d", frame.Function, frame.Line)
}
return ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment