]> Cypherpunks repositories - gostls13.git/commitdiff
os: add examples of environment functions
authorJean-Nicolas Moal <jn.moal@gmail.com>
Mon, 22 Aug 2016 17:02:33 +0000 (19:02 +0200)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 12 Oct 2016 14:04:30 +0000 (14:04 +0000)
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: Brad Fitzpatrick <bradfitz@golang.org>
src/os/example_test.go

index 9c890c451973cee8b65e0f3194f58bdea1e701e9..07f9c76959006b505add5ff3db3684833b26ccde 100644 (file)
@@ -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")
+}