Skip to content

Instantly share code, notes, and snippets.

@ankr
Created August 22, 2018 08:16
Show Gist options
  • Save ankr/4cf5c5b9bdf8f7158e81a9cf67026095 to your computer and use it in GitHub Desktop.
Save ankr/4cf5c5b9bdf8f7158e81a9cf67026095 to your computer and use it in GitHub Desktop.
Go debug function that includes filename and line number
package debug
import (
"fmt"
"runtime"
"strings"
)
func Debug(data interface{}) {
_, file, line, ok := runtime.Caller(1)
if ok == false {
// TODO: panic?
return
}
header := fmt.Sprintf("%s (Line: %d)", file, line)
spacer := fmt.Sprintf("%s", strings.Repeat("-", len(header)))
fmt.Println(spacer)
fmt.Print("\x1b[35;1m") // Debug color
fmt.Println(header)
fmt.Print("\x1b[39;0m") // Reset color
fmt.Println(spacer)
fmt.Printf("%#v\n", data)
fmt.Println(spacer)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment