Logo

Programming-Idioms

History of Idiom 205 > diff from v19 to v20

Edit summary for version 20 by programming-idioms.org:
New Go implementation by user [programming-idioms.org]

Version 19

2019-10-01, 08:34:21

Version 20

2019-10-01, 08:39:29

Idiom #205 Get an environment variable

Read an environment variable with the name "FOO" and assign it to the string variable foo. If it does not exist or if the system does not support environment variables, assign a value of "none".

Idiom #205 Get an environment variable

Read an environment variable with the name "FOO" and assign it to the string variable foo. If it does not exist or if the system does not support environment variables, assign a value of "none".

Extra Keywords
envvar os system
Extra Keywords
envvar os system
Imports
import "os"
Code
foo := os.Getenv("FOO")
if foo == "" {
	foo = "none"
}
Comments bubble
This is fine if empty string means "no value" in your use case.

To distinguish between an empty value and an unset value, use os.LookupEnv.
Doc URL
https://golang.org/pkg/os/#Getenv
Demo URL
https://play.golang.org/p/mFgdB4lXpMR