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

Taught in

References