The Go build system is rejecting all files in your package because a //go:build directive is too restrictive, effectively telling the compiler to ignore everything.

Here are the most common reasons and how to fix them:

1. Mismatched os or arch Tags

You’ve likely specified //go:build darwin (for macOS) or //go:build amd64 (for 64-bit Intel/AMD) when your build environment doesn’t match. The Go toolchain reads these build constraints to decide which files are relevant for a given compilation. If no files match the constraints for your current operating system and architecture, the build will fail.

Diagnosis: Check the GOOS and GOARCH environment variables on your build machine.

go env GOOS GOARCH

Compare this output to the tags in your //go:build comments. For example, if go env shows linux amd64, but your file has //go:build darwin, it won’t be included.

Fix: Remove or modify the offending tags. If you want the file to build everywhere, remove the os and arch tags entirely.

//go:build !amd64 || !darwin
// This is a common mistake: it means "build if NOT amd64 OR NOT darwin".
// If you are on amd64 AND darwin, it will be excluded!

// Correct way to include for all:
// No build tags needed, or use a generic one.

If the file should only be for specific environments, ensure your tags are correct. For example, to build only on Linux:

//go:build linux

This works by including only files that satisfy all specified constraints.

2. Incorrect go Version Constraints

You might have a //go:build directive that specifies a Go version your current Go toolchain doesn’t meet. This is often used to ensure a package is built with a minimum required Go version.

Diagnosis: Check your current Go version.

go version

Compare this to the go tag in your build constraints. If your go version is go1.18, but your constraint is //go:build go1.20, the file will be excluded.

Fix: Update your Go toolchain to match the constraint, or adjust the constraint to match your toolchain. To build for Go 1.18 and later:

//go:build go1.18

If you want the file to be included in all Go versions, remove the go tag.

3. Overly Broad ! (NOT) Logic

The ! operator in build constraints negates a tag. It’s easy to construct logic that unintentionally excludes everything. For example, //go:build !amd64 !darwin means "build if NOT amd64 AND NOT darwin." This will exclude files on both macOS and Linux if they are amd64.

Diagnosis: Carefully analyze the logic of your //go:build lines, especially when using ! or || (OR). Remember that AND is implicit between separate lines, while || and ! are explicit operators.

Fix: Rephrase the logic to be inclusive. To include a file unless it’s on Windows:

//go:build !windows

To include a file only on Linux or macOS:

If you intend to include the file for all platforms, remove the ! logic entirely.

4. Incorrect cgo Usage

Build constraints are often used to conditionally enable or disable cgo. If you have a //go:build directive that implicitly disables cgo (e.g., by not including cgo when it’s expected), and your build depends on cgo, files might be excluded. More commonly, if you don’t want cgo and have a constraint that requires it, it will be excluded.

Diagnosis: Look for //go:build cgo or //go:build !cgo in your files. Understand if your build needs cgo or if it’s being prevented.

Fix: If you need cgo, ensure the constraint allows it. If you don’t need cgo, ensure the constraint reflects that. To enable cgo:

//go:build cgo

To disable cgo:

//go:build !cgo

If you want to use the default behavior (which depends on whether C code is present), omit the cgo constraint.

5. Misplaced or Duplicate Build Files

Sometimes, build constraints are managed by *_test.go files or other build-related files. If you have multiple files with conflicting or overly strict constraints, or if a build file is in the wrong directory, it can lead to unexpected exclusions.

Diagnosis: Search your project for files ending in _test.go and any files named *_arm.go, *_windows.go, etc. Check the //go:build directives within them. Ensure your main source files are not being shadowed by test files or platform-specific files that don’t apply to your build.

Fix: Consolidate or correct the build constraints across all relevant files. Ensure that your primary .go files have the intended constraints and that no other build files are unintentionally excluding them. For example, if you have mycode_linux.go and mycode.go, and you’re building on Linux, the Go toolchain will prefer mycode_linux.go if its constraints match. If mycode_linux.go has an incorrect constraint, it might be excluded, and then the toolchain might fall back to mycode.go which might also have issues.

6. Custom Build Tags Not Met

Beyond standard tags like os, arch, and cgo, you can define custom build tags (e.g., //go:build bigendian or //go:build experimental). If your build environment doesn’t satisfy these custom tags, the files will be excluded.

Diagnosis: Check your go env for custom tags. You can set them via the go build -tags flag.

go build -tags=experimental

If you see custom tags in your //go:build directives, verify they are being provided during the build.

Fix: Either remove the custom tag from the build constraint if it’s not needed, or provide the tag during the build process. To remove the custom tag:

//go:build !experimental

To build with the experimental tag:

go build -tags=experimental .

After fixing these, the next error you’ll likely encounter is a missing main function if the package is intended to be an executable.

Want structured learning?

Take the full Golang course →