Skip to content

Environment variables

Environment variables are key‑value pairs that affect the behaviour of running processes. In Go, you can read them with os.Getenv, set them with os.Setenv, and delete them with os.Unsetenv.

FunctionDescription
os.Getenv(key)Returns the value as a string (empty string if not set).
os.LookupEnv(key)Returns (value, true) if set, otherwise ("", false).
os.Setenv(key, value)Sets an environment variable.
os.Unsetenv(key)Deletes an environment variable.
os.Environ()Returns a slice of "key=value" strings.

Read the HOME directory (or USERPROFILE on Windows).

package main
import (
"fmt"
"os"
)
func main() {
home := os.Getenv("HOME")
if home == "" {
home = os.Getenv("USERPROFILE") // Windows
}
fmt.Println("Home directory:", home)
// Safer way
if value, ok := os.LookupEnv("PATH"); ok {
fmt.Println("PATH is set to:", value[:50]) // print first 50 chars
}
}
Terminal window
Home directory: /home/alice
PATH is set to: /usr/local/bin:/usr/bin:/bin

Environment variables are ideal for configuration (e.g., database URLs, API keys) because they can be changed without recompiling the program.