I am parsing files with the ast package.
I have been looking at the documentation for a bit and I can't find a way to determine if a token is a package declaration, e.g: package main
at the beggining of the file.
func find_package(node ast.Node) bool {
switch x := node.(type) {
// This works with *ast.Ident or *ast.FuncDecl ... but not
// with *ast.Package
case *ast.Package:
fmt.Print(x.Name)
}
return true
}
I am looking for a clean way to do this with the ast package, I am almost sure I am just missing something in the documentation.
So basically, it seems like you have to look for a File
instead of a package:
func find_package(node ast.Node) bool {
switch x := node.(type) {
case *ast.File:
fmt.Print(x.Name)
}
return true
}