Back to .md Directory
48 - Build Constraints and Tags
> Conditional compilation for different platforms and build configurations.
ai
View source48 - Build Constraints and Tags
Conditional compilation for different platforms and build configurations.
📌 What You'll Learn
- Build constraints (build tags)
- Platform-specific code
- Custom build tags
- go:generate directive
🔧 Build Constraints
Build constraints = Compile code conditionally
| Use Case | Example |
|---|---|
| Platform-specific | Windows vs Linux |
| Architecture-specific | amd64 vs arm |
| Build modes | debug vs release |
| Optional features | Custom tags |
Two ways to specify:
- File naming:
file_linux.go,file_windows.go,file_darwin.go - //go:build directive:
//go:build linux && amd64
📝 Platform-Specific Code
// main.go - shared code
package main
func main() {
println(getSystemInfo())
}
// system_linux.go
//go:build linux
package main
func getSystemInfo() string { return "Running on Linux" }
// system_darwin.go
//go:build darwin
package main
func getSystemInfo() string { return "Running on macOS" }
// system_windows.go
//go:build windows
package main
func getSystemInfo() string { return "Running on Windows" }
Output: Varies by GOOS build target (e.g., "Running on Linux")
🏷️ Build Tags Syntax
//go:build linux
//go:build linux || darwin
//go:build linux && amd64
//go:build !windows
//go:build (linux || darwin) && amd64
//go:build debug
go build
go build -tags debug
go build -tags "debug integration"
GOOS=linux go build
GOOS=windows GOARCH=amd64 go build
🎭 Debug vs Release
// debug.go
//go:build debug
package main
func debugLog(msg string) { log.Printf("[DEBUG] %s\n", msg) }
const DebugMode = true
// release.go
//go:build !debug
package main
func debugLog(msg string) {}
const DebugMode = false
go build -tags debug -o myapp-debug
go build -o myapp
⚙️ go:generate
//go:generate stringer -type=Status
//go:generate mockgen -source=interface.go -destination=mock_interface.go
go generate ./...
go generate generate.go
📂 File Naming Conventions
| Pattern | Example | When compiled |
|---|---|---|
*_GOOS.go | file_linux.go | Linux only |
*_GOARCH.go | file_amd64.go | amd64 only |
*_GOOS_GOARCH.go | file_linux_amd64.go | Linux amd64 only |
*_test.go | server_test.go | Test build only |
Common values: GOOS: linux, darwin, windows | GOARCH: amd64, arm64, arm, 386
🎯 Key Takeaways
- //go:build for compile-time constraints
- File naming (
_linux.go) for platform code - Custom tags with
-tagsflag - go generate for code generation
- GOOS/GOARCH for cross-compilation
➡️ Next Steps
Next Topic: 49 - Module Management
Related Documents
CONSTRAINTS.md
DunApp PWA - Project Constraints
> **⚠️ KRITIKUS DOKUMENTUM**
aiclaudeworkflow
0
3
endresztellik-gifCONSTRAINTS.md
Technical Constraints & Requirements
This document defines the technical constraints, requirements, and architecture decisions for the Scottish Mountain Weather App development.
ai
0
2
m-deaneCONSTRAINTS.md
Specifying version constraints
lastupdated: "2025-11-18"
ai
0
2
ibm-cloud-docsCONSTRAINTS.md
Token Constraints Implementation
This document describes the token constraint system implemented to ensure that input to the local LLM (served via Ollama) never exceeds the model's maximum token limit.
aillmrag
0
2
markoverano