Documentation
¶
Overview ¶
Package env provides helpers for reading typed values from environment variables.
Every Get* function accepts a key and a defaultValue. The default is returned when the variable is unset, empty, or (for numeric/duration helpers) unparseable — invalid values are never fatal.
Every MustGet* function accepts only a key and panics when the variable is unset, empty, or (for numeric/duration helpers) unparseable. Use these for required configuration that has no sensible default (e.g. a database URL or API secret) and should fail fast at startup rather than run with a zero value.
Available helpers ¶
- GetString / MustGetString — returns the raw string value
- GetBool / MustGetBool — truthy values are "true"/"1", falsy are "false"/"0"
- GetInt / MustGetInt — parses a decimal integer
- GetFloat / MustGetFloat — parses a 64-bit float
- GetDuration / MustGetDuration — parses a time.Duration string (e.g. "5m", "1h30m")
- GetStringSlice / MustGetStringSlice — splits a comma-separated value; trims whitespace, drops empty elements
Example ¶
port := env.GetInt("PORT", 8080)
debug := env.GetBool("DEBUG", false)
timeout := env.GetDuration("REQUEST_TIMEOUT", 30*time.Second)
origins := env.GetStringSlice("ALLOWED_ORIGINS", []string{"http://localhost:3000"})
dbURL := env.MustGetString("DATABASE_URL")
Package env provides helpers for reading typed values from environment variables.
Index ¶
- func GetBool(key string, defaultValue bool) bool
- func GetDuration(key string, defaultValue time.Duration) time.Duration
- func GetFloat(key string, defaultValue float64) float64
- func GetInt(key string, defaultValue int) int
- func GetString(key string, defaultValue string) string
- func GetStringSlice(key string, defaultValue []string) []string
- func MustGetBool(key string) bool
- func MustGetDuration(key string) time.Duration
- func MustGetFloat(key string) float64
- func MustGetInt(key string) int
- func MustGetString(key string) string
- func MustGetStringSlice(key string) []string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetBool ¶
GetBool returns the boolean value of the environment variable named by key. Truthy values: "true", "1". Any other non-empty value returns defaultValue.
func GetDuration ¶
GetDuration returns the time.Duration value of the environment variable named by key, or defaultValue if the variable is not set, empty, or unparseable.
func GetFloat ¶
GetFloat returns the float64 value of the environment variable named by key. Invalid values are silently ignored and defaultValue is returned instead.
func GetInt ¶
GetInt returns the integer value of the environment variable named by key. Invalid values are silently ignored and defaultValue is returned instead.
func GetString ¶
GetString returns the value of the environment variable named by key, or defaultValue if the variable is not set or empty.
func GetStringSlice ¶
GetStringSlice returns a slice of strings parsed from a comma-separated environment variable. Whitespace around each element is trimmed and empty elements are dropped. Returns defaultValue if the variable is not set or empty.
func MustGetBool ¶
MustGetBool returns the boolean value of the environment variable named by key. It panics if the variable is not set, empty, or not one of "true", "1", "false", "0".
func MustGetDuration ¶
MustGetDuration returns the time.Duration value of the environment variable named by key. It panics if the variable is not set, empty, or not a valid duration.
func MustGetFloat ¶
MustGetFloat returns the float64 value of the environment variable named by key. It panics if the variable is not set, empty, or not a valid float.
func MustGetInt ¶
MustGetInt returns the integer value of the environment variable named by key. It panics if the variable is not set, empty, or not a valid integer.
func MustGetString ¶
MustGetString returns the value of the environment variable named by key. It panics if the variable is not set or empty.
func MustGetStringSlice ¶
MustGetStringSlice returns a slice of strings parsed from a comma-separated environment variable named by key. Whitespace around each element is trimmed and empty elements are dropped. It panics if the variable is not set or empty.
Types ¶
This section is empty.