Skip to content

Instantly share code, notes, and snippets.

@D4ryl00
Last active March 9, 2023 23:18
Show Gist options
  • Save D4ryl00/436782708f4ec7abb10a004645fd1675 to your computer and use it in GitHub Desktop.
Save D4ryl00/436782708f4ec7abb10a004645fd1675 to your computer and use it in GitHub Desktop.
Stack trace
package yourproject
import (
"fmt"
"runtime"
"strings"
)
func getStackFrameInfo(frame int) (string, bool) {
pc, file, line, ok := runtime.Caller(frame)
if !ok {
return "", false
}
rawFuncElems := strings.Split(runtime.FuncForPC(pc).Name(), ".")
function := rawFuncElems[len(rawFuncElems)-1]
return fmt.Sprintf("file: %s, line: %d, function: %s", file, line, function), true
}
func printCleanStack() {
stack := []string{}
for i := 2; ; i++ {
info, ok := getStackFrameInfo(i)
if !ok {
break
}
stack = append([]string{info}, stack...)
}
for _, frame := range stack {
fmt.Println("TOTOTOTO ", frame)
}
fmt.Println("TOTOTOTO")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment