Go - package ast : 在文件中查找包
Go - package ast : find package in file
我正在使用 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:
fmt.Print(x.Name)
}
return true
}
我正在寻找一种干净的方法来使用 ast 包,我几乎可以肯定我只是在文档中遗漏了一些东西。
所以基本上,您似乎必须寻找 File
而不是包裹:
func find_package(node ast.Node) bool {
switch x := node.(type) {
case *ast.File:
fmt.Print(x.Name)
}
return true
}
我正在使用 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:
fmt.Print(x.Name)
}
return true
}
我正在寻找一种干净的方法来使用 ast 包,我几乎可以肯定我只是在文档中遗漏了一些东西。
所以基本上,您似乎必须寻找 File
而不是包裹:
func find_package(node ast.Node) bool {
switch x := node.(type) {
case *ast.File:
fmt.Print(x.Name)
}
return true
}