lean into examples

This commit is contained in:
Mark McGranaghan
2012-10-09 21:02:12 -07:00
parent 5d1775bdaa
commit 8daa226a48
130 changed files with 4 additions and 4 deletions

View File

@@ -0,0 +1,24 @@
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"))
}

View File

@@ -0,0 +1,6 @@
$ go run environment-variables.go
HOME
PATH
PWD
...
bar