envutil
is a tiny Golang library with some utility functions for dealing with
environment variables. It basically exists because I kept copying the same
utility code into my Golang libraries and applications and wanted to lib-ify
this stuff instead of duplicating it.
There are a number of functions named WithDefault{Type}()
which accept the
key of an environment variable and a default value. The function checks to see
if the environs contains the key and, if not, returns the default value. If the
environs does contain the key, then the value of the environment variable
with that key is transformed from a string to the {Type}
mentioned in the
function name.
The functions and their signatures are as follows:
-
WithDefault(key string, def string) string
: Ifkey
isn't found in the environ, returnsdef
-
WithDefaultInt(key string, def int) int
: Ifkey
isn't found in the environ, returnsdef
. Ifkey
is found, converts the environment variable value to anint
. If the conversion fails, returnsdef
. -
WithDefaultBool(key string, def bool) bool
: Ifkey
isn't found in the environ, returnsdef
. Ifkey
is found, converts the environment variable value to anbool
. If the conversion fails, returnsdef
.
package main
import (
"fmt"
"os"
"github.com/jaypipes/envutil"
)
func main() {
fmt.Println("Testing envutil...")
val, ok := os.LookupEnv("SOME_RANDOM_ENV_KEY")
if !ok {
fmt.Println(" SOME_RANDOM_ENV_KEY does not exist in environs.")
} else {
fmt.Printf(" SOME_RANDOM_ENV_KEY exists in environs with value %s\n", val)
}
strval := envutil.WithDefault("SOME_RANDOM_ENV_KEY", "my default str")
fmt.Printf(" WithDefault(\"SOME_RANDOM_ENV_KEY\", \"my default str\") returned '%s'\n", strval)
intval := envutil.WithDefaultInt("SOME_RANDOM_ENV_KEY", 42)
fmt.Printf(" WithDefaultInt(\"SOME_RANDOM_ENV_KEY\", 42) returned %d\n", intval)
fmt.Println(" Setting SOME_RANDOM_ENV_KEY to 'random str'.")
os.Setenv("SOME_RANDOM_ENV_KEY", "random str")
defer os.Unsetenv("SOME_RANDOM_ENV_KEY")
intval = envutil.WithDefaultInt("SOME_RANDOM_ENV_KEY", 42)
fmt.Printf(" WithDefaultInt(\"SOME_RANDOM_ENV_KEY\", 42) returned %d\n", intval)
fmt.Println(" Setting SOME_RANDOM_ENV_KEY to '12'.")
os.Setenv("SOME_RANDOM_ENV_KEY", "12")
intval = envutil.WithDefaultInt("SOME_RANDOM_ENV_KEY", 42)
fmt.Printf(" WithDefaultInt(\"SOME_RANDOM_ENV_KEY\", 42) returned %d\n", intval)
fmt.Println(" Setting SOME_RANDOM_ENV_KEY to 'falsy'.")
os.Setenv("SOME_RANDOM_ENV_KEY", "falsy")
boolval := envutil.WithDefaultBool("SOME_RANDOM_ENV_KEY", true)
fmt.Printf(" WithDefaultBool(\"SOME_RANDOM_ENV_KEY\", true) returned %v\n", boolval)
fmt.Println(" Setting SOME_RANDOM_ENV_KEY to 'false'.")
os.Setenv("SOME_RANDOM_ENV_KEY", "false")
boolval = envutil.WithDefaultBool("SOME_RANDOM_ENV_KEY", true)
fmt.Printf(" WithDefaultBool(\"SOME_RANDOM_ENV_KEY\", true) returned %v\n", boolval)
}
Contributions to envutil
are welcomed! Fork the repo on GitHub and submit a pull
request with your proposed changes. Or, feel free to log an issue for a feature
request or bug report.