parsing - 如何解析方法声明?

标签 parsing go

我正在尝试解析方法声明。基本上,我需要获取接收者基本类型 (type hello) 和返回类型(notypeerror)的语法节点。 ast 包看起来很简单,但由于某种原因我没有得到我需要的数据(即字段报告为零)。

唯一有用的数据似乎只在类型为 interface{}Object -> Decl 字段中提供,所以我认为我无法序列化它。

如有任何帮助,我们将不胜感激。代码如下:

package main

import (
    "fmt"
    "go/ast"
    "go/parser"
    "go/token"
)

func main() {
    // src is the input for which we want to inspect the AST.
    src := `
package mypack
// type hello is a cool type
type hello string

// type notype is not that cool
type notype int

// func printme is like nothing else.
func (x *hello)printme(s string)(notype, error){
    return 0, nil
}
`
    // Create the AST by parsing src.
    fset := token.NewFileSet() // positions are relative to fset
    f, err := parser.ParseFile(fset, "src.go", src, 0)
    if err != nil {
        panic(err)
    }

    // Inspect the AST and find our function
    var mf ast.FuncDecl
    ast.Inspect(f, func(n ast.Node) bool {
        switch x := n.(type) {
        case *ast.FuncDecl:
            mf = *x
        }
        return true
    })

    if mf.Recv != nil {
        fmt.Printf("\n receivers:")
        for _, v := range mf.Recv.List {
            fmt.Printf(",tag %v", v.Tag)
            for _, xv := range v.Names {
                fmt.Printf("name %v, decl %v, data %v, type %v",
                    xv.Name, xv.Obj.Decl, xv.Obj.Data, xv.Obj.Type)
            }
        }
    }
}

Playground

最佳答案

要获得类型,您需要查看 Type 属性,它可以是 ast.StarExprast.Ident

这里看看this :

package main

import (
    "fmt"
    "go/ast"
    "go/parser"
    "go/token"
)

func main() {
    // src is the input for which we want to inspect the AST.
    src := `
package mypack
// type hello is a cool type
type hello string

// type notype is not that cool
type notype int

// printme is like nothing else.
func (x *hello)printme(s string)(notype, error){
    return 0, nil
}
`
    // Create the AST by parsing src.
    fset := token.NewFileSet() // positions are relative to fset
    f, err := parser.ParseFile(fset, "src.go", src, 0)
    if err != nil {
        panic(err)
    }

    // Inspect the AST and find our function
    var mf ast.FuncDecl
    ast.Inspect(f, func(n ast.Node) bool {
        switch x := n.(type) {
        case *ast.FuncDecl:
            mf = *x
        }
        return true
    })

    if mf.Recv != nil {
        for _, v := range mf.Recv.List {
            fmt.Print("recv type : ")
            switch xv := v.Type.(type) {
            case *ast.StarExpr:
                if si, ok := xv.X.(*ast.Ident); ok {
                    fmt.Println(si.Name)
                }
            case *ast.Ident:
                fmt.Println(xv.Name)
            }
        }
    }
}

关于parsing - 如何解析方法声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28246970/

相关文章:

java - 解析 jsoup 错误。根元素为空

c - 解析/proc/maps?

java - 如何从 SOAP 响应中获取单个元素的值? ( java )

gocode 外部包没有给出 vscode 的建议

pointers - 分配的指针字段变为 <nil>

json - 如何在 GO 中包含一个数组作为结构定义的一部分?

parsing - 出于地理编码目的解释文本输入的最佳方法是什么?

java - java中使用多线程解析xml并写入txt文件

unit-testing - 在 Golang 中进行单元测试时如何测试是否调用了 goroutine?

performance - 为什么内存拷贝第一次运行很慢?