Created
August 22, 2018 08:16
-
-
Save ankr/4cf5c5b9bdf8f7158e81a9cf67026095 to your computer and use it in GitHub Desktop.
Go debug function that includes filename and line number
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 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