environment fallback
Using an environment variable as a default for a setting, while still letting an explicit command-line flag override it.
In more detail
The course helper envOr(key, fallback) returns the variable if set, otherwise the fallback, and the result becomes a flag's default. Containers can then be configured through the environment and a person can still override on the command line. Keep the precedence explicit and documented.
Example
addr := flag.String("addr", envOr("PROBE_ADDR", "127.0.0.1:8080"), "address")Related terms
- os.LookupEnv — The Go function that returns an environment variable's value and a boolean saying whether it was set at all.
- command-line option — An argument starting with a dash, such as -r or --help, that changes how a command behaves rather than naming what it works on.
- environment variable — A named setting such as PORT=8000 that a program reads from its surroundings when it starts.
Taught in
- Parsing Arguments with flag — Concurrent Systems Programming with Go
- Project Setup and Configuration — Concurrent Systems Programming with Go