Created
June 8, 2018 03:38
-
-
Save cuiwm/2b0a6e4564340115d5606ee76faa3da4 to your computer and use it in GitHub Desktop.
runtime function
This file contains 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 main | |
import ( | |
"fmt" | |
"runtime" | |
) | |
func trace2() { | |
pc := make([]uintptr, 15) | |
n := runtime.Callers(2, pc) | |
frames := runtime.CallersFrames(pc[:n]) | |
frame, _ := frames.Next() | |
fmt.Printf("%s,:%d %s\n", frame.File, frame.Line, frame.Function) | |
} | |
// Original from https://stackoverflow.com/a/25927915/1490379 | |
func trace() { | |
pc := make([]uintptr, 10) // at least 1 entry needed | |
runtime.Callers(2, pc) | |
f := runtime.FuncForPC(pc[0]) | |
file, line := f.FileLine(pc[0]) | |
fmt.Printf("%s:%d %s\n", file, line, f.Name()) | |
} | |
func main() { | |
trace2() // Returns correct line number (27) | |
trace() // Returns line 29 (should be line 28) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment