lean into examples
This commit is contained in:
24
examples/environment-variables/environment-variables.go
Normal file
24
examples/environment-variables/environment-variables.go
Normal 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"))
|
||||
}
|
||||
6
examples/environment-variables/environment-variables.sh
Normal file
6
examples/environment-variables/environment-variables.sh
Normal file
@@ -0,0 +1,6 @@
|
||||
$ go run environment-variables.go
|
||||
HOME
|
||||
PATH
|
||||
PWD
|
||||
...
|
||||
bar
|
||||
Reference in New Issue
Block a user