I am rewriting in Go a home-grade program of mine that was previously in Python, learning on the way. As I reconsider whole sections of the code, it would be great if I could ignore some .go
files I wrote (they throw compilation errors which is OK as they are not synchronized with my latest approach to the problem).
I could move them out of the directory, or rename them but I was wondering whether there would be a more idiomatic way to ignore some .go
files in a module directory?
Build tags fit perfectly to your use case.
A Build Constraint or also known as Build Tag is used to include or exclude files in a package during a build process. With this, we can build different types of builds from the same source code.
So if you want to exclude a file from the build process, give it a different Build Tag. For example if you use
//go:build exclude
at the top of the file when you performgo build
it won't be included. Note thatexclude
can be any word.For more details on the Build Tags check out the following article by Dave Cheney here
TLDR
to the top of the file you wish to ignore.