Golang / GoLang Production Patterns and Web Standards Interview Questions
How do you manage environment-specific settings and feature flags in Go?
Production Go applications distinguish between environments (development, staging, production) through configuration — not through build tags or conditional compilation. Feature flags allow gradual rollouts without redeployment.
// Environment detection via config type Environment string const ( EnvDevelopment Environment = "development" EnvStaging Environment = "staging" EnvProduction Environment = "production" ) type Config struct { Env Environment Debug bool LogLevel slog.Level // ... } func loadConfig() *Config { env := Environment(os.Getenv("APP_ENV")) if env == "" { env = EnvDevelopment } cfg := &Config{Env: env} switch env { case EnvProduction: cfg.LogLevel = slog.LevelInfo cfg.Debug = false default: cfg.LogLevel = slog.LevelDebug cfg.Debug = true } return cfg } // Simple feature flag implementation type FeatureFlags struct { mu sync.RWMutex flags map[string]bool } func (f *FeatureFlags) IsEnabled(name string) bool { f.mu.RLock() defer f.mu.RUnlock() return f.flags[name] } func (f *FeatureFlags) Set(name string, enabled bool) { f.mu.Lock() defer f.mu.Unlock() f.flags[name] = enabled } // In a handler: func userHandler(w http.ResponseWriter, r *http.Request) { if flags.IsEnabled("new-user-dashboard") { renderNewDashboard(w, r) return } renderLegacyDashboard(w, r) }
More Related questions...