Go's standard library covers most configuration needs without third-party code: os and flag read environment variables and command-line flags, encoding/json parses JSON files, and embed bundles defaults into the binary. It has no built-in YAML or TOML parser and no infrastructure-as-code (IaC) framework, so for those you add a small library or call tools such as Terraform from Go.
The reliable pattern is to define one typed Config struct, fill it in layers (defaults, file, environment, flags), validate it once at startup and pass it to the rest of the program explicitly.
| Need | Standard library package | Notes |
|---|---|---|
| Environment variables | os (Getenv, LookupEnv) | LookupEnv tells an empty value apart from an unset one |
| Command-line flags | flag | Typed flags with defaults and generated -h help |
| JSON files | encoding/json | Use os.ReadFile; io/ioutil is deprecated since Go 1.16 |
| Defaults shipped in the binary | embed | Embed a defaults.json with //go:embed |
| Logging the effective settings | log/slog | Structured logs, available since Go 1.21 |
| YAML, TOML, .env | Not included | Use a maintained third-party package |
Read the file with os.ReadFile and decode it with a json.Decoder that rejects unknown fields. A misspelt key then fails at startup instead of being silently ignored.
Starting from a struct that already holds defaults means the file only needs to contain the values that differ. Return errors instead of calling panic, so the caller decides how to report them.
The twelve-factor convention is that environment variables override the file and flags override both. Write one small helper per type and apply the layers in order.
Because the flag defaults are taken from the environment-derived values, running ./app -port 9000 overrides APP_PORT, and omitting the flag keeps the environment value.
Validate once, right after loading, and report every problem together. errors.Join (Go 1.20 and later) collects them into a single error.
Never log secrets when you print the effective configuration. Give secret fields a String or LogValue method that masks them.
Go does not ship a YAML or TOML parser. The usual choices are github.com/BurntSushi/toml for TOML and a maintained YAML module such as gopkg.in/yaml.v3 or its successor. Keep the same struct and swap the tag (toml:"port" or yaml:"port"). Check that the module is still maintained before adding it. Larger projects sometimes adopt a layered configuration library, but a typed struct plus the layering above is enough for most services.
For secrets, prefer environment variables injected by your platform, mounted files such as Kubernetes secrets read with os.ReadFile, or a secrets manager. Do not commit them to the repository or embed them in the binary.
The standard library has no IaC support, but Go is the language most IaC tooling is written in, and there are three practical ways to use it.
os/exec or an official client library.Always use exec.CommandContext so the command can be cancelled, pass arguments separately and never build a shell string from user input.
This runs plan and writes a plan file for review. The command from the older version of this answer, terraform apply -auto-approve, applies changes without review and belongs only in a pipeline that has already approved the plan.
When the infrastructure is a Kubernetes cluster, use the official k8s.io/client-go module instead of shelling out to kubectl. The example below lists pods using the local kubeconfig.
Inside a cluster use rest.InClusterConfig() instead, so the program uses its service account and the permissions granted by RBAC rather than a kubeconfig file.
No. encoding/json, encoding/xml, encoding/csv and encoding/gob are in the standard library, but YAML and TOML need a third-party module.
Use both. Keep non-secret structure in a file with defaults, and let environment variables or flags override per deployment. Secrets belong in environment variables or a secrets manager.
ioutil.ReadFile discouraged?The io/ioutil package has been deprecated since Go 1.16. Use os.ReadFile, os.WriteFile and io.ReadAll, which behave the same.
Not directly, but Pulumi lets you describe infrastructure in Go, and Go programs can drive Terraform through os/exec. Terraform providers themselves are written in Go.
Define one typed configuration struct, fill it from defaults, a JSON file, environment variables and flags, and validate it at startup. Use os.ReadFile, DisallowUnknownFields and errors.Join for safe loading. For infrastructure as code, use Go through Pulumi, provider development or exec.CommandContext, and use client-go for Kubernetes.