Something went wrong. Try again.
Like Rust's dbg macro, but for Go!
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121// Package debug provides an easy mechanism to enable and streamline "print debugging"// inspired by Rust's [dbg macro].//// [dbg macro]: https://doc.rust-lang.org/stable/std/macro.dbg.htmlpackage debug // import "go.followtheprocess.codes/debug"
import ( "bytes" "fmt" "go/ast" "go/format" "go/parser" "go/printer" "go/token" "os" "runtime")
// Debug prints the file, line number and the value to stderr in a nice human-readable format.//// Any internal error is printed to stderr and the process halted.//// something := "hello"// debug.Debug(something) // DEBUG: [/Users/you/projects/myproject/main.go:30:3] something = "hello"func Debug(value any) { _, file, line, ok := runtime.Caller(1) // Skip: 1 so this file gets skipped if !ok { fmt.Fprintln(os.Stderr, "DEBUG: Unable to determine caller")
return }
// Parse the file in question fset := token.NewFileSet()
f, err := parser.ParseFile(fset, file, nil, parser.ParseComments) if err != nil { fmt.Fprintf(os.Stderr, "DEBUG: Failed to parse %s: %v\n", file, err)
return }
ast.Inspect(f, findAndDebug(fset, line, value))}
// findAndDebug is an AST traversal function to be passed to [go/ast.Inspect]// and will find calls to debug.Debug, and print the debugging information.//// It returns a closure that can be passed directly to [go/ast.Inspect].func findAndDebug(fset *token.FileSet, line int, value any) func(node ast.Node) bool { return func(node ast.Node) bool { if node == nil { return false }
start := fset.Position(node.Pos())
// We already know the line from runtime.Caller so if it's not the right line, keep looking if start.Line != line { return true }
// If the node we're currently visiting is not a function call, keep looking call, ok := node.(*ast.CallExpr) if !ok { return true }
// If the function call is not a call to debug.Debug, keep looking if !isDebugCall(call.Fun) { return true }
// By now we know it's a function call, and we know the function the user is calling // is debug.Debug
// Debug takes a single argument and it's not variadic so len(parsed.Args) == 1 is enforced // at compile time arg := call.Args[0] val := fmt.Sprintf("%#v", value)
// Pretty print the arg node to display it buf := &bytes.Buffer{} printer.Fprint(buf, fset, arg)
// Run go fmt on the value to ensure a canonical format formatted, err := format.Source([]byte(val)) if err != nil { // If we couldn't format it nicely, just print the raw value fmt.Fprintf(os.Stderr, "DEBUG: [%v] %v = %v\n", fset.Position(call.Fun.Pos()), buf.String(), val) } else { // We could format the value with go fmt, so use that fmt.Fprintf( os.Stderr, "DEBUG: [%v] %v = %s\n", fset.Position(call.Fun.Pos()), buf.String(), string(formatted), ) }
return false // Found it }}
// isDebugCall takes an arbitrary AST expression and determines if it// was a call to debug.Debug.func isDebugCall(expr ast.Expr) bool { selector, ok := expr.(*ast.SelectorExpr)
return ok && isIdent(selector.X, "debug") && isIdent(selector.Sel, "Debug")}
// isIdent takes an arbitrary AST expression and a name and determines if// the expression was assigned to that name.func isIdent(expr ast.Expr, name string) bool { ident, ok := expr.(*ast.Ident)
return ok && ident.Name == name}