Gopath Janitor

I put together a simple tool last week that you may find useful.

The idea is that over time, your GOPATH can become cluttered with all of the various items you’ve acquired via go get. The go tool does a nice job at fetching the dependencies of commands for you, so getting the command and its source installed is a piece of cake.
Removing a command is not so easy. Sure, you can remove $GOPATH/src/<CMD> and $GOPATH/bin/<CMD>, but what about all of those dependencies?
Wouldn’t it be nice if you could instantly locate unused packages in your GOPATH?

This may not be a new idea, but I did a quick search and couldn’t find such a tool, so I decided to build it. The tool is called gpj, short for Gopath Janitor, and you can find it on github.

There’s not much to it - the go/build package can do most of the work for you. All I had to do was gather up all of the “library” packages, and then loop through all packages in GOPATH, marking each package’s imports as being used. Any library packages that aren’t marked as imported get reported to the user.

I used the neat asciinema tool to generate an example session. Check it out:


Future Enhancements

In the future, I’d like to do a better job with internal and vendored packages. For now, the tool assumes that you don’t want to hear about these types of packages.

One last thing to note is that you shouldn’t just go removing any directories that are displayed in the gpj output. It’s important to understand that gpj is looking for unimported packages. Many repositories consist of multiple packages. If you have a dependency on one of those packages, go get is going to grab the entire repository and you’ll end up with some unimported packages in your GOPATH.