Golang / GoLang Production Patterns and Web Standards Interview Questions
How do build tags work in Go and when do you use them?
Build constraints (build tags) allow you to include or exclude source files from compilation based on OS, architecture, Go version, or custom conditions. They are used for platform-specific code and integration test separation.
// Modern syntax (Go 1.17+) — //go:build directive
// Must be the first non-blank, non-comment line in the file
// OS-specific implementation
// File: signals_unix.go
//go:build linux || darwin
package server
import "syscall"
func shutdownSignals() []os.Signal {
return []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP}
}
// File: signals_windows.go
//go:build windows
package server
func shutdownSignals() []os.Signal {
return []os.Signal{os.Interrupt}
}
// Integration test exclusion
// File: integration_test.go
//go:build integration
package api_test
// Run only with: go test -tags integration ./...
func TestDatabaseIntegration(t *testing.T) {
db := openRealDB(t)
// ...
}
// Constraint operators:
// //go:build linux && amd64 — AND
// //go:build linux || darwin — OR
// //go:build !windows — NOT
// //go:build go1.21 — minimum Go version
// Filename convention (alternative, no build tag needed):
// file_linux.go — only on Linux
// file_windows_amd64.go — only on Windows/amd64
// file_test.go — only in test builds
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
