return fallback
While not an official Go standard library feature, the pattern of using a .env.go.local file has emerged as a best practice among elite Go teams. It bridges the gap between the inflexibility of hardcoded constants and the chaos of global environment variables.
# Ignore all local overrides *.go.local This keeps production secrets and developer-specific paths out of version control. Using //go:embed with .env.go.local For static assets, you can embed a local configuration: .env.go.local
// config/env.go.local package config func LocalOverrides() os.Setenv("PORT", "3000")
// You can even add validation here. println("⚠️ Running in LOCAL mode with development config.") To run your application with .env.go.local active: return fallback While not an official Go standard
//go:build local // +build local package config
One of the hidden benefits is test isolation. Create config/env_test.go.local : Using //go:embed with
.PHONY: dev dev: go run -tags local ./cmd/server .PHONY: build-prod build-prod: go build -o bin/server ./cmd/server Ensure your .gitignore prevents accidental commits: