Environment Variables
Fast recall
Quick revision
Core answer
Whether you understand Environment Variables beyond memorized syntax.
Giving a definition without explaining a practical trade-off.
Connect the concept to a small production example and state its complexity or failure mode.
Interview-ready guide
Interview answer
The functions, executed:
os.Setenv("MYVAR", "hello")
os.Getenv("MYVAR") // "hello" — verified
os.Getenv("NOTSET") // "" — same as an empty var: ambiguous
os.LookupEnv("MYVAR") // ("hello", true) — present
os.LookupEnv("NOTSET") // ("", false) — ABSENT — the distinctionThe two reading forms: Getenv returns a string — empty for unset AND empty-set (you can't tell). LookupEnv returns (string, bool) — the bool is 'was it set' — the form for when empty-vs-unset matters (an empty DATABASE_URL might mean 'use a default'; an unset one might mean 'not configured — fail').
The convention: config values live in env vars (12-factor). The pattern:
if v, ok := os.LookupEnv("PORT"); ok {
port = v
} else {
port = "8080" // default
}Setting: os.Setenv — process-local (the process's environment; doesn't affect the parent or siblings). Useful for testing (set up an env-dependent function's input) and for child-process configuration (os/exec inherits the current process's environment).
The type discipline: env vars are strings — numbers need strconv.Atoi(port), time.ParseDuration(d.Timeout), URLs url.Parse — with the error handling that goes with parsing.
Environment Variables
Direct Answer
os.Getenv(key) → string (empty if unset), os.LookupEnv(key) → (value, ok — distinguishes unset from empty), os.Setenv(key, value), os.Environ() → all.
Deep Dive & Architecture
The environment inheritance model: a Go process starts with the parent's environment (the shell's exported variables, the container's ENV, the Kubernetes configMap's entries). Setenv mutates the process's own copy — children (exec.Command) inherit it, parents don't see it. The lifecycle: process-local, no persistence.
The Environ() inventory: os.Environ() returns a []string of KEY=VALUE pairs — the complete environment. The pattern for debugging and for filtering (find all MYAPP_*-prefixed vars). The companion: os.ExpandEnv("$HOME/dir") — shell-style expansion within a string.
The godotenv pattern (the .env convention): the library loads a .env file into the process's env at startup — the dev-time convenience that the container's ENV directives replace in production. The pattern to know: the APP reads env vars (never the .env file directly); the loading is a startup step (main or init) — the config flow stays env-var-shaped.
Common Mistakes & Pitfalls
Giving a definition without explaining a practical trade-off.
Check your understanding
Answer these without looking back at the article.
- When would you use Environment Variables in a real project?
- How would you compare Environment Variables with an alternative?
- What is a common mistake when using Environment Variables?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.