Skip to content

Commit 6d702d8

Browse files
jnmoalbradfitz
authored andcommitted
os: add examples of environment functions
For #16360. Change-Id: Iaa3548704786018eacec530f7a907b976fa532fe Reviewed-on: https://go-review.googlesource.com/27443 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent b65cdc2 commit 6d702d8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/os/example_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,46 @@ func ExampleIsNotExist() {
6161
// Output:
6262
// file does not exist
6363
}
64+
65+
func init() {
66+
os.Setenv("USER", "gopher")
67+
os.Setenv("HOME", "/usr/gopher")
68+
os.Unsetenv("GOPATH")
69+
}
70+
71+
func ExampleExpandEnv() {
72+
fmt.Println(os.ExpandEnv("$USER lives in ${HOME}."))
73+
74+
// Output:
75+
// gopher lives in /usr/gopher.
76+
}
77+
78+
func ExampleLookupEnv() {
79+
show := func(key string) {
80+
val, ok := os.LookupEnv(key)
81+
if !ok {
82+
fmt.Printf("%s not set\n", key)
83+
} else {
84+
fmt.Printf("%s=%s\n", key, val)
85+
}
86+
}
87+
88+
show("USER")
89+
show("GOPATH")
90+
91+
// Output:
92+
// USER=gopher
93+
// GOPATH not set
94+
}
95+
96+
func ExampleGetenv() {
97+
fmt.Printf("%s lives in %s.\n", os.Getenv("USER"), os.Getenv("HOME"))
98+
99+
// Output:
100+
// gopher lives in /usr/gopher.
101+
}
102+
103+
func ExampleUnsetenv() {
104+
os.Setenv("TMPDIR", "/my/tmp")
105+
defer os.Unsetenv("TMPDIR")
106+
}

0 commit comments

Comments
 (0)