Listing Go source files for a given package and its dependencies


reading Daniel’s post on driving a Go build from a makefile I wondered if there was a better method to generate the dependencies, and came up with this solution. I’m sure it can be polished and be made more correct (for example, this implementation ignores assembler files).

The idea is to take a package’s import path, and use that to list the package’s dependencies. The packages in $GOROOT are pruned (because they don’t usually change). Then for the input package and all the imported packages, the Go source files are listed. Since multiple packages might import the same package, the list is sorted and deduplicated.

There’s the subtlety that if the number of dependencies is really large, the script might end up calling the last bit multiple times, listing the source files for the input package multiple times. The deduplication would take care of that.

The output of this script can be used in a makefile to decide if a binary needs to be rebuilt or not. I would expect go install to perform better, though, and I wonder if there’s a large open source Go code base to try this on. Juju is what immediately comes to mind. Is there something larger?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.