Parsing environment variables with Go is simple, thanks to the standard library os package.

os.Getenv() gets an environment variable. It’s not possible to determine not set or empty. Use os.LookupEnv() to be able to do that.

foo := os.Getenv("FOO")

os.Setenv() sets an environment variable.

os.Setenv("FOO", "Something")

os.Unsetenv() unsets an environment variable.

os.Unsetenv("FOO")

os.Clearenv() unsets all environment variables.

os.Clearenv()

os.Environ() returns a slice of strings containing all the environment variables in key=value format.

vars := os.Environ()

os.ExpandEnv() given a string, expands $VAR environment variables entries to the corresponding value.

s := os.ExpandEnv("$FOO is amazing")

os.LookupEnv() returns the value of the environment variable in its first parameter if set, otherwise the second parameter is false. Allows to distinguish unset from empty value.

foo, ok := os.LookupEnv("FOO")