Skip to content

Instantly share code, notes, and snippets.

@csthompson
Created September 11, 2020 23:01
Show Gist options
  • Save csthompson/d4b44c91c8d1894733ecad1f1bfcdc01 to your computer and use it in GitHub Desktop.
Save csthompson/d4b44c91c8d1894733ecad1f1bfcdc01 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
)
func main() {
//Create a new token set for the file
fset := token.NewFileSet()
//Parse the file and create an AST
file, err := parser.ParseFile(fset, "../nats_publisher/main.go", nil, parser.ParseComments)
if err != nil {
panic(err)
}
ast.Inspect(file, func(n ast.Node) bool {
// Find Return Statements
funcCall, ok := n.(*ast.CallExpr)
if ok {
mthd, ok := funcCall.Fun.(*ast.SelectorExpr)
if ok {
_, ok = mthd.X.(*ast.Ident)
if ok {
if mthd.Sel.Name == "Publish" && mthd.X.(*ast.Ident).Name == "ec" {
fmt.Printf("Topic: %s \n", funcCall.Args[0].(*ast.BasicLit).Value)
}
}
}
}
return true
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment