我正在学习如何使用以及 golang 的 ast 库是如何工作的。我正在解析https://github.com/modern-go/concurrent,避免测试文件和 go_below_19.go 因为它会导致错误。
我的问题是解析文件中的这些行unbounded_executor.go,
var HandlePanic = func(recovered interface{}, funcName string) {
ErrorLogger.Println(fmt.Sprintf("%s panic: %v", funcName, recovered))
ErrorLogger.Println(string(debug.Stack()))
}
Run Code Online (Sandbox Code Playgroud)
ErrorLogger两个实例中的 ast.Ident都有一个 nil obj。
但是,我认为它不应该是 nil 并且应该引用这些行log.go,
// ErrorLogger is used to print out error, can be set to writer other than stderr
var ErrorLogger = log.New(os.Stderr, "", 0)
Run Code Online (Sandbox Code Playgroud)
我错了,还是解析器有问题?我遵循了几个关于解析文件的参考资料,并在每个文件中重用了 *token.FileSet 并使用 ParseComments 作为模式。
编辑:
围绕此有大量代码库,因此演示此内容的代码将包含片段。
这是在所有非测试 go 文件中使用相同的 fset 执行的,没有会阻止代码与 1.16 一起使用的构建限制
parsedFile, parseErr := parser.ParseFile(fset, filePath, nil, parser.ParseComments)
Run Code Online (Sandbox Code Playgroud)