【问题标题】:Go - package ast : find package in fileGo - package ast:在文件中查找包
【发布时间】:2016-11-01 20:09:07
【问题描述】:

我正在使用 ast 包解析文件。
我一直在查看文档,但找不到确定令牌是否是包声明的方法,例如package main at the begining文件。

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 包 的简洁方法,我几乎可以肯定我只是在文档中遗漏了一些东西。

【问题讨论】:

    标签: parsing go package abstract-syntax-tree


    【解决方案1】:

    所以基本上,您似乎必须寻找 File 而不是包:

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

    https://golang.org/pkg/go/ast/#File

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 2021-12-08
      • 1970-01-01
      • 2017-09-20
      • 1970-01-01
      相关资源
      最近更新 更多