Skip to content

Instantly share code, notes, and snippets.

@tsavola
Created August 27, 2018 16:57
Show Gist options
  • Save tsavola/b2ebecd67d927104e970998a3392bfb5 to your computer and use it in GitHub Desktop.
Save tsavola/b2ebecd67d927104e970998a3392bfb5 to your computer and use it in GitHub Desktop.
TODO() which prints the source code location
//go:noinline
func TODO(...interface{}) {
_todo()
// panic("TODO")
}
//go:noinline
func _todo() {
funcName := "<unknown function>"
fileName := "<unknown file>"
fileLine := 0
pc := make([]uintptr, 1)
n := runtime.Callers(3, pc)
if n > 0 {
f, _ := runtime.CallersFrames(pc[:n]).Next()
funcName = f.Function
fileName = f.File
fileLine = f.Line
}
if i := strings.LastIndex(fileName, "/"); i >= 0 {
fileName = fileName[i+1:]
}
if i := strings.LastIndex(funcName, "."); i >= 0 {
funcName = funcName[i+1:]
}
debug.Printf("TODO in %s (%s:%d)", funcName, fileName, fileLine)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment