A simple getenv utility to support default values to be set. This is a port from python's os.getenv("ENV_KEY", "default)
. This utility function will do a few things:
- Return the passed default value (with a logging message).
- Return the environment variable set over the default value
- Panic if the Environment variable does not exist and no default value set.
Installation is done using the go get command:
go get -u github.com/roger-king/go-getenv
Using the utility function is simple. We call the function and pass in as the first arg the Environment Variable Key
and the getenv.String("some-string")
.
import "github.com/roger-king/go-getenv"
port := getenv.EnvOrDefault("PORT", getenv.String(":5000"))
I decided to create a String()
function because I want to support the ability to pass nil
as the second or default argument.