How to Exclude Files from `go get` in Golang
Grace Collins
Solutions Engineer · Leapcell

Key Takeaways
- Use Go build constraints (
//go:build tag
) to exclude files from compilation. - Separate directories with
go.mod
to prevent inclusion in builds and documentation. - Excluded files are ignored unless explicitly included using
-tags
during the build process.
In Go, managing which files are included during the build process is essential for maintaining a clean and efficient codebase. While the go get
command is primarily used for downloading and installing packages, controlling the inclusion of specific files during the build is achieved through build constraints, also known as build tags. These directives allow developers to include or exclude files from the build based on specified conditions.
Understanding Build Constraints
Build constraints are special comments placed at the top of Go source files that dictate when a file should be included in the package during the build process. They are particularly useful for excluding files that are not relevant in certain build contexts.
Syntax of Build Constraints
For Go versions 1.17 and later, the syntax for a build constraint is:
//go:build [condition]
For versions earlier than 1.17, the syntax is:
// +build [condition]
These lines must appear before the package declaration in the source file.
Excluding Files from the Build
To exclude a file from the build, you can define a custom build tag. For example, to exclude a file using the tag exclude
, add the following line at the top of the file:
//go:build exclude
For versions below 1.17:
// +build exclude
With this directive, the Go toolchain will ignore the file unless the exclude
tag is explicitly specified during the build. To build including files with the exclude
tag, use:
go build -tags=exclude
By default, running go build
without the -tags=exclude
flag will exclude these files from the build.
Practical Example
Suppose you have a file debug.go
that contains debugging utilities not intended for production builds. At the top of debug.go
, add:
//go:build debug
When building for production, simply run:
go build
This command will exclude debug.go
from the build. For a development build that includes debugging utilities, use:
go build -tags=debug
Excluding Directories
To exclude an entire directory, such as examples
, from the build and documentation, you can place a separate go.mod
file inside the examples
directory. This approach treats the directory as a separate module, preventing it from being included in the main build process. As discussed in the Go Forum, adding a go.mod
file to the examples
directory effectively isolates it from the main module.
Conclusion
While go get
focuses on retrieving and installing packages, controlling which files are included during the build is managed through build constraints. By effectively using build tags and structuring your project with considerations like separate go.mod
files for example directories, you can maintain a clean and efficient build process in Go.
FAQs
Add //go:build [tag]
at the top of the file and avoid specifying the tag during build.
Yes, by placing a go.mod
file inside the directory, treating it as a separate module.
Files with build constraints will be excluded unless explicitly included with -tags
.
We are Leapcell, your top choice for hosting Go projects.
Leapcell is the Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis:
Multi-Language Support
- Develop with Node.js, Python, Go, or Rust.
Deploy unlimited projects for free
- pay only for usage — no requests, no charges.
Unbeatable Cost Efficiency
- Pay-as-you-go with no idle charges.
- Example: $25 supports 6.94M requests at a 60ms average response time.
Streamlined Developer Experience
- Intuitive UI for effortless setup.
- Fully automated CI/CD pipelines and GitOps integration.
- Real-time metrics and logging for actionable insights.
Effortless Scalability and High Performance
- Auto-scaling to handle high concurrency with ease.
- Zero operational overhead — just focus on building.
Explore more in the Documentation!
Follow us on X: @LeapcellHQ