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)
}
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...
