Documentation
¶
Overview ¶
Package loadenv provides easy access to env variables from .env file. Also, loadenv provides option to pass default environments to use, if searched variable will not be found.
Lists (GetEnvAsSlice, IsStringIsValidSlice) follow one contract: a single element is a valid list, elements are trimmed at both ends, empty elements are dropped, and the default value is returned only when nothing is left — the variable is unset, empty, or built of separators alone. An empty separator also gives the default value: splitting by it would yield a slice of characters. The price of trimming is that a value whose edge whitespace is significant will not reach the caller intact; write such whitespace as an escape (`\s`, `[ ]`) rather than a literal.
Index ¶
- func GetEnv(key, defaultVal string) string
- func GetEnvAsBool(name string, defaultVal bool) bool
- func GetEnvAsInt(name string, defaultVal int) int
- func GetEnvAsSlice(name string, defaultVal []string, separator string) []string
- func Init(paths ...string)
- func IsStringIsValidSlice(str, separator string) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetEnvAsBool ¶
GetEnvAsBool is a helper to read an environment variable into a bool or return default value.
func GetEnvAsInt ¶
GetEnvAsInt is a helper function to read an environment variable into integer or return a default value.
func GetEnvAsSlice ¶
GetEnvAsSlice is a helper function to read an environment variable into a slice or return a default value.
Elements are trimmed and empty ones are dropped, so "a,b" and "a, b" with a "," separator give the same result. The default value is returned only when nothing is left: the variable is unset, empty, or built of separators alone.
A single element is a valid slice. It used to be replaced by the default value without a word, and that is the case every "one origin", "one host", "one allowed method" setting falls into — that is, the one people actually write.
func Init ¶ added in v1.12.0
func Init(paths ...string)
Init подразумевает явный вызов и инициализацию переменных с указанным путем перед поулчением переменных окружения.
func IsStringIsValidSlice ¶
IsStringIsValidSlice is a helper to check, if string is a valid slice to convert it into []string. Can also be used outside loadenv package for other cases.
One element is enough: a list of one is still a list. The former "more than one" rule made every single-valued setting silently fall back to its default.
Types ¶
This section is empty.