Environment Variables
package main
import (
"fmt"
"os"
"strings"
)
func main() {
// Set the value of the "FOO" environment variable to "1"
os.Setenv("FOO", "1")
// Retrieve and print the value of the "FOO" environment variable
fmt.Println("FOO:", os.Getenv("FOO"))
// Retrieve and print the value of the "BAR" environment variable (not set)
fmt.Println("BAR:", os.Getenv("BAR"))
fmt.Println()
// Iterate through all environment variables and print their names
for _, e := range os.Environ() {
// Split the environment variable into a key-value pair
pair := strings.SplitN(e, "=", 2)
// Print the name of the environment variable
fmt.Println(pair[0])
}
}Last updated