Documentation
¶
Overview ¶
Package env provides typed accessors for environment variables with fallback defaults.
Get* functions return the parsed value or a fallback; MustGet* functions panic when the variable is not set. Every accessed key is recorded with its type and default, which can be inspected through RequiredKeys, Defaults, Types and TypeOf. All functions are safe for concurrent use.
port := env.GetInt("PORT", 8080)
token := env.MustGet("API_TOKEN")
Index ¶
- func Defaults() map[string]any
- func Exists(key string) bool
- func Get(key, fallback string) string
- func GetBool(key string, fallback bool) bool
- func GetFloat64(key string, fallback float64) float64
- func GetInt(key string, fallback int) int
- func GetInt64(key string, fallback int64) int64
- func GetIntSlice(key string, fallback []int) []int
- func GetStringSlice(key string, fallback []string) []string
- func MustExists(key string)
- func MustGet(key string) string
- func MustGetBool(key string) bool
- func MustGetFloat64(key string) float64
- func MustGetGetIntSlice(key string) []int
- func MustGetInt(key string) int
- func MustGetInt64(key string) int64
- func MustGetStringSlice(key string) []string
- func Readme() stringdeprecated
- func RequiredKeys() []string
- func TypeOf(key string) string
- func Types() map[string]string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
Get returns the value of the environment variable key, or fallback if it is not set. The first fallback seen for key is recorded as its default.
func GetBool ¶
GetBool returns the environment variable key parsed with strconv.ParseBool, or fallback if it is not set or cannot be parsed.
func GetFloat64 ¶ added in v0.10.21
GetFloat64 returns the environment variable key parsed as float64, or fallback if it is not set or cannot be parsed.
func GetInt ¶
GetInt returns the environment variable key parsed as int, or fallback if it is not set or cannot be parsed.
func GetInt64 ¶ added in v0.10.21
GetInt64 returns the environment variable key parsed as base-10 int64, or fallback if it is not set or cannot be parsed.
func GetIntSlice ¶
GetIntSlice returns the environment variable key split on "," and parsed as ints, or fallback if it is unset, empty or any element cannot be parsed.
func GetStringSlice ¶
GetStringSlice returns the environment variable key split on ",", or fallback if it is unset or empty. Elements are not trimmed.
func MustExists ¶
func MustExists(key string)
MustExists panics if the environment variable key is not set and otherwise records key as required.
func MustGet ¶
MustGet returns the value of the environment variable key and panics if it is not set.
func MustGetBool ¶
MustGetBool returns the environment variable key parsed as bool and panics if it is not set. An unparsable value yields false.
func MustGetFloat64 ¶ added in v0.10.21
MustGetFloat64 returns the environment variable key parsed as float64 and panics if it is not set. An unparsable value yields 0.
func MustGetGetIntSlice ¶
MustGetGetIntSlice returns the environment variable key as []int (see GetIntSlice) and panics if it is not set.
func MustGetInt ¶
MustGetInt returns the environment variable key parsed as int and panics if it is not set. An unparsable value yields 0.
func MustGetInt64 ¶ added in v0.10.21
MustGetInt64 returns the environment variable key parsed as int64 and panics if it is not set. An unparsable value yields 0.
func MustGetStringSlice ¶
MustGetStringSlice returns the environment variable key split on "," and panics if it is not set.
func RequiredKeys ¶ added in v0.17.0
func RequiredKeys() []string
RequiredKeys returns all keys recorded as required, in unspecified order.
Types ¶
This section is empty.