Configuration management in Go is about how your program gets its settings, such as a port, a database host or a feature flag. Environment management is one source of those settings: environment variables set by the operating system, a container or an orchestrator. Config files hold structured, versionable defaults, while environment variables carry values that change per deployment or must stay out of the repository, such as secrets. Most production Go services use both, with a clear order of precedence.
Go has no built-in configuration framework. The standard library gives you os, encoding/json, flag and log/slog, and you combine them into a small loader. This article shows how each approach works, where they differ, and how to layer them safely.
Configuration management is the practice of describing settings in a structured form, usually a file (JSON, YAML or TOML) that is decoded into a Go struct. The file lives next to the code or is mounted into the container, so it can be reviewed, diffed and versioned.
A good config struct has these properties:
int and a timeout is a time.Duration.The standard library supports JSON out of the box. YAML and TOML need a third-party package, because Go ships no parser for them.
Use os.ReadFile (Go 1.16 and later). The old io/ioutil package is deprecated, and ioutil.ReadFile is only a wrapper around os.ReadFile.
The struct is pre-filled with defaults, and json.Unmarshal overwrites only the keys present in the file. A matching config.json looks like this:
Note that the original version of this example stored a database password in the file. Do not commit passwords to a config file; read them from the environment or a secret store instead.
Environment variables are key-value strings attached to the process. Set them in the shell, in a Dockerfile, in a Kubernetes manifest or in a systemd unit, then read them with the os package.
os.Getenv returns an empty string for both "unset" and "set to empty". Use os.LookupEnv when you must tell the two apart. Every value is a string, so you convert and validate it yourself.
| Aspect | Config file | Environment variables |
|---|---|---|
| Structure | Nested, typed, can hold lists and maps | Flat strings only |
| Best for | Defaults, feature flags, non-secret settings | Per-environment values and secrets |
| Versioning | Easy to review and diff in Git | Lives in the deployment system, not in the code |
| Change without rebuild | Yes, if mounted or edited on disk | Yes, by restarting with new values |
| Risk | Secrets committed by accident | Visible in process listings and crash dumps if mishandled |
| Fits twelve-factor apps | Partly | Yes, this is the recommended place for deploy-specific settings |
Neither is better in general. The question to ask for each setting is who changes it and how often: developers (file) or operators (environment).
The usual pattern is: built-in defaults, then the config file, then environment variables, then command-line flags. Later layers win. This lets you override a single value in production without editing the file.
Running PORT=9000 go run . prints {Port:9000 Host:localhost}, and PORT=9000 go run . -port 7000 prints {Port:7000 Host:localhost}.
Hand-written loaders are fine for small services. For larger ones, packages such as github.com/caarlos0/env (struct tags for environment variables) or github.com/knadh/koanf (layered sources) remove the boilerplate. Pick one, keep the config struct as the single source of truth, and do not read os.Getenv from random places in the code.
main, validate it, and pass the struct to the components that need it. Avoid global state.log/slog, implement slog.LogValuer on a secret type so that it prints as redacted.Use both. Keep structured, non-secret defaults in a file and override per-deployment values and secrets with environment variables. Precedence should be documented and the same in every environment.
The standard library only decodes JSON. Use a maintained third-party decoder for YAML or TOML, or a layered library such as koanf, and decode into the same typed struct.
ioutil.ReadFile still fine to use?It still works but io/ioutil has been deprecated since Go 1.16. Use os.ReadFile instead, which behaves the same way.
They are better than committing secrets, but any process with access to the environment can read them, and they may leak into logs or child processes. For high-sensitivity secrets prefer a secret manager or files with strict permissions.
Configuration files give you structured, versioned settings that decode into typed Go structs. Environment variables give you flat, per-deployment values and a safe place for secrets. Combine them with defaults, a file, environment variables and flags in that order, validate once at startup, and never log secret values.