登录
首页 >  Golang >  Go问答

go/types: Config.Check 找不到包

来源:stackoverflow

时间:2024-02-24 20:51:23 106浏览 收藏

今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《go/types: Config.Check 找不到包》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!

问题内容

我正在使用 go/types 包作为解析 go 源代码的工具。我的代码如下所示:

packagename := "github.com/something/my-test-package"
imported, err := build.default.import(packagename, ".", build.findonly)
if err != nil {
    return nil, errors.wrapf(err, "error importing package %s", packagename)
}

packages, err := parser.parsedir(fileset, imported.dir, nil, 0)
if err != nil {
    return nil, errors.wrapf(err, "error parsing package %s", packagename)
}

for _, astpkg := range packages {
    var files []*ast.file
    for _, file := range astpkg.files {
        files = append(files, file)
    }

    info := &types.info{
        defs: make(map[*ast.ident]types.object),
    }

    conf := types.config{
        importer: importer.forcompiler(fileset, "source", nil),
    }

    _, err := conf.check(packagename, fileset, files, info)
    if err != nil {
        panic(err) // panics here
    }
}

在上面的示例中,即使我的代码构建良好,我也会从 conf.check 收到错误。错误是:

panic: /Users/home/Dev/my-test-package/bindings/bindings.go:6:2: could not import github.com/something/my-test-package/prototype (type-checking package "github.com/something/my-test-package/prototype" failed (/Users/home/Dev/my-test-package/prototype/basic.go:3:8: could not import  could not import github.com/golang/protobuf/ptypes/wrappers (type-checking package "github.com/golang/protobuf/ptypes/wrappers" failed (/Users/home/go/pkg/mod/github.com/golang/[email protected]/ptypes/wrappers/wrappers.pb.go:6:14: could not import github.com/golang/protobuf/proto (cannot find package "github.com/golang/protobuf/proto" in any of:
    /usr/local/go/src/github.com/golang/protobuf/proto (from $GOROOT)
    /Users/home/go/src/github.com/golang/protobuf/proto (from $GOPATH))))))))))))))))

最终错误是:无法导入github.com/golang/protobuf/proto

如果我从同一个包运行 go list -f '{{ .dir }}' github.com/golang/protobuf/proto ,我会得到 /users/home/go/pkg/mod/github.com/ golang/[email protected]/proto,很明显该包可用。我在 golang 存储库中发布了一个错误,但我想我也应该在这里发布,以防我做一些明显错误的事情。


解决方案


这似乎是一个已知问题,或与已知问题相关:tools that use GOPATH need special updates to work with go modules

一些 issues similar to yours 已与该问题相关联。

好了,本文到此结束,带大家了解了《go/types: Config.Check 找不到包》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>