Go has no built-in configuration framework, so you assemble one from the standard library: environment variables with os.Getenv, command-line flags with the flag package, and config files decoded with encoding/json (or a third-party YAML or TOML library). A common production rule is to load defaults, then override them from a file, then from environment variables, then from flags, and to validate the result once at startup.
Follow the twelve-factor approach for deployed services: keep configuration in the environment, keep secrets out of source control, and fail fast when required settings are missing.
| Source | Best for | Notes |
|---|---|---|
| Defaults in code | Safe fallback values | Always provide them |
| Config file (JSON, YAML, TOML) | Structured, non-secret settings | Easy to review and version |
| Environment variables | Per-environment values and secrets | Standard in containers and Kubernetes |
| Command-line flags | Overrides and CLI tools | Built into the flag package |
| Secret manager | Passwords, API keys, tokens | Vault, cloud secret stores, mounted files |
Each later source in the list normally overrides the earlier one, so a flag beats an environment variable, which beats a file, which beats a default.
Reading variables is a one-liner, but parse and validate them in one place.
os.LookupEnv tells you whether a variable is set, which os.Getenv cannot, because an unset and an empty variable both return an empty string. Failing at startup is better than failing on the first request.
The flag package defines typed flags with defaults and help text.
Flags suit command-line tools and one-off overrides. For larger CLIs, libraries such as cobra add subcommands, while pflag adds POSIX-style flags.
A JSON file mapped to a struct with tags is the simplest structured option. Note that ioutil.ReadFile is deprecated since Go 1.16; use os.ReadFile.
The standard library has no YAML or TOML decoder; common choices are gopkg.in/yaml.v3 and github.com/BurntSushi/toml, used the same way with struct tags (yaml:"port", toml:"port").
Never store real passwords in a committed file. Put the secret in an environment variable or secret manager and reference it.
For apps with several sources, libraries save boilerplate:
spf13/viper reads files, environment variables and flags, and can watch files for changes.kelseyhightower/envconfig and caarlos0/env fill a struct from environment variables using tags.knadh/koanf is a lighter layered alternative.A sketch using caarlos0/env:
Choose a library when the manual code grows; the standard library is enough for many services.
Config struct and pass it into constructors rather than reading os.Getenv all over the code.time.Duration and typed fields instead of raw strings.Config value directly instead of manipulating the process environment, or use t.Setenv when you must.For most services, a typed struct filled from defaults, an optional file and environment variables, validated at startup. Add a library such as viper only when you need many sources or live reload.
Use environment variables for per-environment values and secrets, and files for larger structured settings. Many apps use both and let the environment override the file.
Watch the file or listen for a signal such as SIGHUP, reload into a new struct, validate it, then swap it in atomically with atomic.Pointer. Many settings, such as listen ports, still need a restart.
ioutil.ReadFile still fine?It still works but is deprecated since Go 1.16. Use os.ReadFile instead.
Manage Go configuration with a typed struct, defaults, an optional file, environment variables and flags, in that override order, and validate it once at startup. Use os.ReadFile and encoding/json for files, flag for command-line options, keep secrets in the environment or a secret manager, and reach for viper or env libraries only when the standard library stops being enough.