gobyexample/050-env.go
Mark McGranaghan 354a9d862f reorder
2012-09-21 07:54:20 -07:00

28 lines
767 B
Go

package main // Use the `os` package to list, set, and get
// environment variables.
import (
"os"
"fmt"
"strings"
)
func main() {
for _, e := range os.Environ() { // `Environ` returns a slice of strings in the form
pair := strings.Split(e, "=") // `KEY=value`. You can `strings.Split` them to get
fmt.Println(pair[0]) // the key and value.
}
fmt.Println()
os.Setenv("FOO", "bar") // `Setenv` sets a given key, value pair.
fmt.Println(os.Getenv("FOO")) // `Getenv` returns the value at key, or an empty
} // string if the key isn't present.
/*
$ go run env.go
HOME
PATH
PWD
...
bar
*/