From: Jean-Nicolas Moal Date: Mon, 22 Aug 2016 17:02:33 +0000 (+0200) Subject: os: add examples of environment functions X-Git-Tag: go1.8beta1~919 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=6d702d8ed2a980cd0c96499379eee5eb218f8339;p=gostls13.git os: add examples of environment functions For #16360. Change-Id: Iaa3548704786018eacec530f7a907b976fa532fe Reviewed-on: https://go-review.googlesource.com/27443 Run-TryBot: Russ Cox TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- diff --git a/src/os/example_test.go b/src/os/example_test.go index 9c890c4519..07f9c76959 100644 --- a/src/os/example_test.go +++ b/src/os/example_test.go @@ -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") +}