Python’s configparser maps to JSON/TOML/YAML via libraries, or simple encoding/json for INI-like needs when you control the format.
type Config struct {
Addr string `json:"addr"`
Port int `json:"port"`
}
data, err := os.ReadFile("config.json")
if err != nil {
return err
}
var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
return err
}
Load file first, then overlay os.Getenv for secrets and deployment-specific values.
Common in Go ecosystem: github.com/pelletier/go-toml/v2 for TOML files.
Parse flag after loading defaults from file so CLI wins:
flag.Parse()
if *addrFromFlag != "" {
cfg.Addr = *addrFromFlag
}
Validate required fields after decode; return wrapped errors listing missing keys.
Rare: watch file mtime or use SIGHUP; keep concurrency-safe access to config with atomic/sync.RWMutex.