Skip to content

Instantly share code, notes, and snippets.

@chriskirkland
Created September 16, 2019 15:55
Show Gist options
  • Save chriskirkland/9faead5cd627ed50818e6af39fc4e7b8 to your computer and use it in GitHub Desktop.
Save chriskirkland/9faead5cd627ed50818e6af39fc4e7b8 to your computer and use it in GitHub Desktop.
print AST for Go file
package main
import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
)
func main() {
src, err := ioutil.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
// Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, "", string(src), 0)
if err != nil {
panic(err)
}
// Print the AST.
ast.Print(fset, f)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment