mirror of
https://github.com/golang/go
synced 2024-11-18 14:44:41 -07:00
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: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
b65cdc2888
commit
6d702d8ed2
@ -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")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user