package main import ( "bytes" "fmt" "go/ast" "go/parser" "go/printer" "go/token" "runtime" ) func main() { assert(1 < 2) assert(1 > 2) } func assert(cond bool) { if cond { return } pc, file, line, ok := runtime.Caller(0) fmt.Println(pc, file, line, ok) fset := token.NewFileSet() f, err := parser.ParseFile(fset, file, nil, parser.ParseComments) if err != nil { panic(err) } ast.Inspect(f, func(node ast.Node) bool { if callExpr, ok := node.(*ast.CallExpr); ok { ident, ok := callExpr.Fun.(*ast.Ident) if !ok { return true } if ident.Name == "assert" { // TODO: use runtime.FuncForPC var buf bytes.Buffer printer.Fprint(&buf, fset, callExpr.Args[0]) fmt.Println("args", buf.String()) } } return true }) } // Output: // 5112365 /tmp/main.go 25 true // args 1 < 2 // args 1 > 2