How to Exclude a Directory as a Package in Golang
Daniel Hayes
Full-Stack Engineer · Leapcell

Key Takeaways
- A directory is treated as a package only if it contains
.go
files. - Build constraints (
// +build tag
) can exclude files from compilation. - IDEs like GoLand allow directory exclusion to prevent indexing.
In Go, any directory containing one or more Go source files (.go
files) is considered a package. However, there are scenarios where you might want to exclude a directory from being treated as a package. This can be achieved by ensuring that the directory does not contain any Go source files or by using build constraints to prevent the files from being included in the build.
1. Removing Go Source Files from the Directory
The simplest way to exclude a directory from being recognized as a package is to remove all .go
files from that directory. Without Go source files, the Go toolchain will not treat the directory as a package.
2. Using Build Constraints
If you need to keep Go source files in the directory but want to exclude them from the build process, you can use build constraints (also known as build tags). Build constraints are special comments that instruct the Go compiler to include or exclude files based on specified conditions.
To exclude a file from the build, add a build constraint at the top of the file that specifies a condition that is not met during the build. For example, you can define a custom build tag exclude
and use it as follows:
// +build exclude package yourpackage // Your code here
By default, the exclude
tag is not defined, so the Go compiler will ignore this file during the build process. To include the file, you would need to specify the exclude
tag during the build:
go build -tags exclude
For more information on build constraints, refer to the official Go documentation:
https://golang.org/pkg/go/build/#hdr-Build_Constraints
3. Excluding Directories in GoLand
If you're using GoLand as your integrated development environment (IDE), you can exclude directories from the project to prevent them from being treated as packages. This can be useful to avoid indexing or to prevent accidental modifications.
To exclude a directory in GoLand:
- Right-click on the directory in the Project tool window.
- Select
Mark Directory as
>Excluded
.
This action will exclude the directory from the project view and the build process.
By utilizing these methods, you can effectively exclude directories from being treated as packages in your Go projects.
FAQs
No, Golang recognizes packages by file content, not directory names.
No, if build constraints are applied, they won’t be included in the build.
Use .gitignore
or exclude directories in build scripts.
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