Skip to content

Instantly share code, notes, and snippets.

@koesie10

koesie10/ast.go Secret

Created September 5, 2018 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koesie10/ba6af59e0dd8213260e5944c1464b0b1 to your computer and use it in GitHub Desktop.
Save koesie10/ba6af59e0dd8213260e5944c1464b0b1 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"log"
"os"
)
func main() {
src := []byte(`package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
`)
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "", src, 0)
if err != nil {
log.Fatal(err)
}
ast.Inspect(file, func(n ast.Node) bool {
call, ok := n.(*ast.CallExpr)
if !ok {
return true
}
printer.Fprint(os.Stdout, fset, call.Fun)
fmt.Println()
return false
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment