How to ignore a .go file in a module?

12.1k views Asked by At

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?

1

There are 1 answers

1
Sai Ravi Teja K On BEST ANSWER

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 perform go build it won't be included. Note that exclude can be any word.

For more details on the Build Tags check out the following article by Dave Cheney here

TLDR

Add

//go:build exclude for go v1.17 and above

//+build exclude for go v1.17 and below

to the top of the file you wish to ignore.