Documentation ¶
Overview ¶
Package go-decouple is inspired by the python-decouple package (https://github.com/henriquebastos/python-decouple). It provides a layuer above gotdotenv (https://github.com/joho/godotenv) that handles defaults and type conversion.
For example, if you want to read an integer value from an environment variable, you can call:
decouple.Load() value, exists = decouple.GetInt("MY_INT_VAR", 0)
If MY_INT_VAR exists, 'value' will be set to the integer value and 'exists' will be true. If MY_INT_VAR does not exist, 'value' will be set to 0 and 'exists' will be false. If MY_INT_VAR exists but cannot be converted into the requested type, 'value' will be set to 0 and 'exists' will be false.
Index ¶
- func GetBool(name string, defval bool) (bool, bool)
- func GetInt(name string, defval int) (int, bool)
- func GetIntInRange(name string, defval, minval, maxval int) (int, bool)
- func GetString(name, defval string) (string, bool)
- func Load(filenames ...string) error
- func LookupEnv(name string) (string, bool)
- func SetPrefix(prefix string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetBool ¶
GetBool returns the value of an environment varilable as a boolean.
If the named variable exists, attempt to convert it to a bool. If the conversion is successful, return (value, true). If the conversion fails or if the named variable does not exist, return (defval, false).
Example:
debugMode, _ := decouple.GetBool("DEBUG_MODE")
func GetInt ¶
GetInt returns the value of an environment variable as an int.
If the named variable exists, attempt to convert it to an integer. If the conversion is successful, return (value, true). If the conversion fails or if the named variable does not exist, return (defval, false).
Example:
widgetCount, _ := decouple.GetInt("WIDGET_COUNT", 10)
func GetIntInRange ¶
GetIntInRange returns the value of environment variable as an int, clamped to an explicit range.
If the named variable exists, attempt to convert it to an integer. If the conversion is successful and the value falls within the given range, return (value, true). If value > maxval, return (maxval, true). If value < minval, return (minval, true). If the conversion fails or if the named variable does not exist, return (defval, false).
Example:
logLevel, _ := decouple.GetIntInRange("LOG_LEVEL", 1, -1, 5)
func GetString ¶
GetString returns the value of an environment variable as a string.
If the named variable exists, return the tuple (value, true). If the named variable does not exist, reutrn the tuple (defval, false).
Example:
configpath, _ := decouple.GetString("CONFIG_PATH", "/home/.config/myconfig.yaml")
func Load ¶
Load is a proxy for godotenv.Load. It will load environment variables from the named files, or from '.env' if no filenames are provided.
Load variables from '.env':
decouple.Load()
Load variables from 'production.env':
decouple.Load("production.env")
Types ¶
This section is empty.