Commit 6d702d8e authored by Jean-Nicolas Moal's avatar Jean-Nicolas Moal Committed by Brad Fitzpatrick

os: add examples of environment functions

For #16360.

Change-Id: Iaa3548704786018eacec530f7a907b976fa532fe
Reviewed-on: https://go-review.googlesource.com/27443
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent b65cdc28
......@@ -61,3 +61,46 @@ func ExampleIsNotExist() {
// Output:
// file does not exist
}
func init() {
os.Setenv("USER", "gopher")
os.Setenv("HOME", "/usr/gopher")
os.Unsetenv("GOPATH")
}
func ExampleExpandEnv() {
fmt.Println(os.ExpandEnv("$USER lives in ${HOME}."))
// Output:
// gopher lives in /usr/gopher.
}
func ExampleLookupEnv() {
show := func(key string) {
val, ok := os.LookupEnv(key)
if !ok {
fmt.Printf("%s not set\n", key)
} else {
fmt.Printf("%s=%s\n", key, val)
}
}
show("USER")
show("GOPATH")
// Output:
// USER=gopher
// GOPATH not set
}
func ExampleGetenv() {
fmt.Printf("%s lives in %s.\n", os.Getenv("USER"), os.Getenv("HOME"))
// Output:
// gopher lives in /usr/gopher.
}
func ExampleUnsetenv() {
os.Setenv("TMPDIR", "/my/tmp")
defer os.Unsetenv("TMPDIR")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment