简体   繁体   中英

Is there a way to get ALL packages and dependencies of Go programs in a given directory and its subdirectories using Go standard tooling?

I have a large directory of Go packages which is actually my $GOPATH/src directory. I am trying to get a list of all packages (and their versions eventually if possible, but even names would be a nice start) within all subdirectories of that directory recursively (including the vendor directories). When I run go list all ./... in $GOPATH/src, I get a bunch of messages saying:

code in directory [foo] expects import [bar]

However, if I navigate to the individual package working directories, it seems to work. I am also not sure why it actually does find the packages in the above command, but displays that error, which it does not display when I navigate down to each package's directory. Is there a way to specify such a recursive lookup using the Go toolset?

I am also not sure why it actually does find the packages in the above command, but displays that error, which it does not display when I navigate down to each package's directory

That seems to be the consequence of src/cmd/go/internal/load/pkg.go

        if !cfg.ModulesEnabled && data.err == nil &&
            data.p.ImportComment != "" && data.p.ImportComment != path &&
            !strings.Contains(path, "/vendor/") && !strings.HasPrefix(path, "vendor/") {
            data.err = fmt.Errorf("code in directory %s expects import %q", data.p.Dir, data.p.ImportComment)
        }

In a non-module-enabled project, with vendor/ packages, you would get that error (as opposed to the same command run inside the vendor folder, which would not trigger the condition shown above)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM