env
This commit is contained in:
parent
dd7a95399b
commit
80f5ba3e00
40
051-env.go
40
051-env.go
@ -1,21 +1,27 @@
|
|||||||
package main
|
package main // Use the `os` package to list, set, and get
|
||||||
|
// environment variables.
|
||||||
import ("os"; "fmt"; "strings")
|
import (
|
||||||
|
"os"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
for _, e := range os.Environ() {
|
for _, e := range os.Environ() { // `Environ` returns a slice of strings in the form
|
||||||
pair := strings.Split(e, "=")
|
pair := strings.Split(e, "=") // `KEY=value`.
|
||||||
fmt.Println(pair[0], pair[1])
|
fmt.Println(pair[0])
|
||||||
}
|
}
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
|
||||||
fmt.Println(os.Getenv("PWD"))
|
os.Setenv("FOO", "bar") // `Setenv` sets a given key, value pair.
|
||||||
fmt.Println()
|
fmt.Println(os.Getenv("FOO")) // `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"))
|
$ go run env.go
|
||||||
}
|
HOME
|
||||||
|
PATH
|
||||||
// == todo
|
PWD
|
||||||
// ensure pattern
|
...
|
||||||
// link to 12 factor
|
bar
|
||||||
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user