相关疑难解决方法(0)

跨包使用 go/parser

我使用go/parser来解析 golang 文件并检查它的 AST。我有一个特定的问题,我想使用 go/parser 但我遇到了障碍。

考虑以下文件存在于 GOPATH/src 中

$GOPATH/src/
    example.go
    example_package/
        example_package.go
Run Code Online (Sandbox Code Playgroud)

以下是上述文件的内容

例子.go

package main

import (
    "example_package"
)

type MyObject struct {
    base *example_package.BaseObject
}

func DoMyThing(arg *example_package.FirstArg) {
    arg.Write(10)
}

func DoMyAnotherThing() {
}

func main() {
    example_package.GetItStarted(&MyObject{})
}
Run Code Online (Sandbox Code Playgroud)

example_package.go

package example_package

func GetItStarted(obj interface{}) {
}

type FirstArg interface {
    Read() int
    Write(x int)
}

type BaseObject struct {
}

func (p *BaseObject) DoSomething(arg *FirstArg, a int) {
    arg.Write(arg.Read() + a)
}
Run Code Online (Sandbox Code Playgroud)

我的意图是编写一个名为 …

parsing code-generation abstract-syntax-tree go

2
推荐指数
1
解决办法
932
查看次数