Skip to content

Instantly share code, notes, and snippets.

@odeke-em
Created August 21, 2016 03:16
Show Gist options
  • Save odeke-em/d73b783c937f46fcd15fe17ef9127984 to your computer and use it in GitHub Desktop.
Save odeke-em/d73b783c937f46fcd15fe17ef9127984 to your computer and use it in GitHub Desktop.
File, Function name and line information for debugging call sites
package main
import (
"fmt"
"runtime"
)
func debugPrintf(fmt_ string, args ...interface{}) {
programCounter, file, line, _ := runtime.Caller(1)
fn := runtime.FuncForPC(programCounter)
prefix := fmt.Sprintf("[%s:%s %d] %s", file, fn.Name(), line, fmt_)
fmt.Printf(prefix, args...)
fmt.Println()
}
func foo() {
debugPrintf("calling here")
}
func foo2() {
debugPrintf("called here")
foo()
}
func main() {
foo2()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment