os.LookupEnv
The Go function that returns an environment variable's value and a boolean saying whether it was set at all.
In more detail
Use the comma ok result to tell a missing variable from one set to an empty string. It is the building block of an environment fallback such as envOr. Prefer it over os.Getenv whenever absent needs its own behaviour.
Example
if v, ok := os.LookupEnv("PROBE_DEBUG"); ok {
fmt.Println("debug:", v)
}Related terms
- os.Getenv — The Go function that returns the value of an environment variable, or an empty string if it is not set.
- comma ok idiom — The Go pattern of receiving a second true-or-false result, as in value, ok := m[key], to learn whether an operation really found something.
- environment fallback — Using an environment variable as a default for a setting, while still letting an explicit command-line flag override it.
- environment variable — A named setting such as PORT=8000 that a program reads from its surroundings when it starts.
Taught in
- Files and the os Package — Concurrent Systems Programming with Go
- Parsing Arguments with flag — Concurrent Systems Programming with Go