Skip to content

Instantly share code, notes, and snippets.

@lukaszx0
Last active February 23, 2021 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaszx0/e7727e15f63ff48f887779160b1aeac3 to your computer and use it in GitHub Desktop.
Save lukaszx0/e7727e15f63ff48f887779160b1aeac3 to your computer and use it in GitHub Desktop.
const maxStackLength = 10
const skipCallers = 5
func stacktrace() string {
stackBuf := make([]uintptr, maxStackLength)
length := runtime.Callers(skipCallers, stackBuf[:])
stack := stackBuf[:length]
trace := ""
frames := runtime.CallersFrames(stack)
for {
frame, more := frames.Next()
if strings.Contains(frame.File, "runtime/") || strings.Contains(frame.File, "vendor/") {
continue
}
pwd, _ := os.Getwd()
trace = trace + fmt.Sprintf("\n\tFile: %s Line: %d. Function: %s", strings.TrimPrefix(frame.File, pwd+"/"), frame.Line, frame.Function)
if !more {
break
}
}
return trace
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment