Parsing Go-package ast:在文件中查找包
我正在使用ast包解析文件 我已经看了文档一段时间了,我找不到一种方法来确定令牌是否是包声明,例如:文件开头的Parsing Go-package ast:在文件中查找包,parsing,go,package,abstract-syntax-tree,Parsing,Go,Package,Abstract Syntax Tree,我正在使用ast包解析文件 我已经看了文档一段时间了,我找不到一种方法来确定令牌是否是包声明,例如:文件开头的package main 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:
package main
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
}
我正在寻找一种干净的方法来使用ast包实现这一点,我几乎可以肯定,我只是在文档中遗漏了一些东西。因此,基本上,您似乎必须查找
文件而不是包:
func find_package(node ast.Node) bool {
switch x := node.(type) {
case *ast.File:
fmt.Print(x.Name)
}
return true
}