我需要解析一个Go源代码文件,找到一个特定的类型(按名称),并在我的程序中使用它。我已经设法使用go/ast包找到了我需要的类型,但我不知道如何将它“加载”到我的程序中以便我可以使用它。
问:从外部源代码文件中提取和使用类型并在运行时使用它的最佳方式是什么?
我想不到任何东西,除了一个丑陋的方法,基本上复制文件,修改它注入一个“主”函数与我的编码东西,发送结果到stdOut,执行它,从标准输出收集编码数据,删除修改后的文件。
用例:分析go源代码并将类型编码为特定格式(例如,json schema)
编辑:这是一些代码。问题是如何对type allTypes (零值)进行编码,然后打印出来。
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"encoding/json"
)
var src string = `
package mypack
type allTypes struct{
Brands Brands
Colours Colours
}
type Brands struct{
Sony string
Apple string
}
type Colours struct{
Red string
Green string
}
`
type sometype struct{
Nothing int
}
func main() {
// src is the input for which we want to inspect the AST.
// 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 tp ast.TypeSpec
ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.TypeSpec:
if x.Name.Name == "allTypes"{
tp = *x
}
}
return true
})
fmt.Printf("We found the type: it is %v", tp)
// Encode the zero value of sometype
x := sometype{}
b, _ := json.Marshal(&x)
fmt.Printf("\n Zero value of someType (json) %s", b)
//Next/Question: How to encode the zero value of "allTypes" ???
}也在playground上
发布于 2015-02-03 13:22:01
如果我理解你是要求动态类型加载,就像Java的Class.forName(String className)一样。简短的答案是Go不支持这一点。
正如Nick Johnson指出的那样,正确的方法是使用ast解析树,然后自己“生成”JSON。您将无法“加载”该类型并使用JSON.Marshal。同样值得注意的是,任何支持json.Marshaler接口的类型都可以生成自定义的JSON。您还需要忽略,但标记为可选的"omitempty“行为。这确实阻止了您使用编译它并通过"stdout“行为进行黑客攻击。
发布于 2015-01-31 16:00:28
如果需要在运行时提取类型信息,则需要使用reflect包。这就是Go的encoding/json和其他类似包的工作方式。
发布于 2015-01-31 18:22:58
如果您想要对Go源文件中定义的类型进行操作,您可以使用go.parser包来读取源文件并将其解析为AST,然后遍历AST以查找您想要检查的元素。
https://stackoverflow.com/questions/28249366
复制相似问题