Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Parsing Go-package ast:在文件中查找包_Parsing_Go_Package_Abstract Syntax Tree - Fatal编程技术网

Parsing Go-package 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:

我正在使用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包实现这一点,我几乎可以肯定,我只是在文档中遗漏了一些东西。

因此,基本上,您似乎必须查找
文件而不是包:

func find_package(node ast.Node) bool {
    switch x := node.(type) {
    case *ast.File:
        fmt.Print(x.Name)
    }
    return true
}