gobyexample/061-environment-variables/environment-variables.go
2012-09-23 18:31:33 -07:00

27 lines
705 B
Go

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