Environment variables
Context
Section titled “Context”Environment variables are key‑value pairs that configure the operating system environment. In Go, use os.Getenv to read them, os.Setenv to set them, and os.Unsetenv to delete them.
Example
Section titled “Example”Read the HOME variable and a custom APP_MODE variable.
Code example
Section titled “Code example”package main
import ( "fmt" "os")
func main() { home := os.Getenv("HOME") fmt.Println("Home:", home)
mode := os.Getenv("APP_MODE") if mode == "" { mode = "development" } fmt.Println("Mode:", mode)
// Lookup with ok if val, ok := os.LookupEnv("PATH"); ok { fmt.Println("PATH exists") }}Output
Section titled “Output”Home: /home/userMode: developmentPATH exists