Last active
March 9, 2023 23:18
-
-
Save D4ryl00/436782708f4ec7abb10a004645fd1675 to your computer and use it in GitHub Desktop.
Stack trace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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