Environment Variables
In this page:
value := os.Getenv("NAME")
value, ok := os.LookupEnv("NAME")
os.Setenv("NAME", "value")
Reading an Environment Variable
os.Getenv retrieves the value of a named environment variable, returning an empty string if it isn't set -- simple, but it can't distinguish 'not set' from 'set to empty'.
Example: Reading an Environment Variable
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"fmt"
"os"
)
// main is where the program starts running
func main() {
os.Setenv("APP_MODE", "production")
// Short declaration: create mode and infer the type
mode := os.Getenv("APP_MODE")
// Print the values followed by a newline
fmt.Println("running in mode:", mode)
}
Login to try C/C++/Java/PHP code in the editor
Checking If a Variable Is Set
os.LookupEnv returns both the value and a boolean indicating whether the variable was actually present, letting you distinguish an intentionally empty value from a missing one.
Example: Checking If a Variable Is Set
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"fmt"
"os"
)
// main is where the program starts running
func main() {
// Short declaration: create val, ok and infer the type
val, ok := os.LookupEnv("MISSING_VAR")
// Print the values followed by a newline
fmt.Println("value:", val, "was set:", ok)
}
Login to try C/C++/Java/PHP code in the editor
Setting a Default When Unset
A common pattern is reading a configuration value from the environment and falling back to a sensible default if it isn't set, avoiding a program crash for optional settings.
Example: Setting a Default When Unset
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"fmt"
"os"
)
// Define the function getEnvOrDefault
func getEnvOrDefault(key, fallback string) string {
// Check whether val, ok := os.LookupEnv(key); ok
if val, ok := os.LookupEnv(key); ok {
// Send val back to the caller
return val
}
// Send fallback back to the caller
return fallback
}
// main is where the program starts running
func main() {
// Short declaration: create port and infer the type
port := getEnvOrDefault("PORT", "8080")
fmt.Println("using port:", port)
}
Login to try C/C++/Java/PHP code in the editor
- Assuming os.Getenv returns an error for a missing variable -- it silently returns an empty string instead.
- Not using os.LookupEnv when you need to distinguish an unset variable from one deliberately set to an empty string.
- Hardcoding sensitive values (like API keys) directly in source code instead of reading them from the environment.
- os.Getenv(name) reads an environment variable, returning an empty string if it's unset.
- os.LookupEnv(name) additionally returns a boolean indicating whether the variable was actually set.
- os.Setenv sets an environment variable for the current process (and its children).
- Environment variables are a common way to pass configuration into a program without hardcoding it.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: