Atomic Config Reload Swap
Reload configuration atomically using atomic.Value for zero-downtime config updates.
Code
package main
import ("sync/atomic";"time";"fmt")
type Config struct {
Port int
FeatureFlags map[string]bool
}
var config atomic.Value
func loadConfig() Config { return config.Load().(Config) }
func reloadConfig(newCfg Config) { config.Store(&newCfg) }
func main() {
config.Store(&Config{Port: 8080})
go func() {
for { time.Sleep(time.Minute); reloadConfig(Config{Port: 9090}) }
}()
fmt.Println(loadConfig().Port)
}Report a problem with this exampleOpens a pre-filled issue on GitHub.