Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetWithFallback ¶
GetWithFallback retrieves an environment variable and tries to convert it to the desired type. If the environment variable is not set or the conversion fails, it returns the provided default value.
Example:
// Get the environment variable "MY_VAR" as a string. // If the variable is not set or an error occurs, return "default". value := env.GetWithFallback("MY_VAR", "default") // Get the environment variable "MY_VAR" as an integer. // If the variable is not set or an error occurs, return 42. value := env.GetWithFallback("MY_VAR", 42) // Get the environment variable "MY_VAR" as a [time.Duration] using a custom converter. // If the variable is not set or an error occurs, return 5 * time.Second. value := env.GetWithFallback("MY_VAR", 5*time.Second, time.ParseDuration)
func MustGet ¶
MustGet retrieves an environment variable and tries to convert it to the desired type. If the environment variable is not set or the conversion fails, it panics.
Example:
// Get the environment variable "MY_VAR" as a string and panic if an error occurs. value := env.MustGet[string]("MY_VAR") // Get the environment variable "MY_VAR" as an integer and panic if an error occurs. value := env.MustGet[int]("MY_VAR") // Get the environment variable "MY_VAR" as a [time.Duration] using a custom converter and panic if an error occurs. value := env.MustGet[time.Duration]("MY_VAR", time.ParseDuration)
Types ¶
type OptionalVariable ¶
type OptionalVariable[T any] interface{ TypedVariable[T] }
OptionalVariable represents an optional environment variable.
It always returns a valid value, either the value of the environment variable or the fallback value if the environment variable is not set or an error occurs.
type RequiredVariable ¶
type RequiredVariable[T any] interface{ TypedVariable[T] }
RequiredVariable represents a required environment variable.
The environment variable must be set and always be a valid value.
type TypedVariable ¶
type TypedVariable[T any] interface { // Convert sets a custom converter function to convert the string value of the environment variable to type T. // If no converter is set, it tries to resolve a default converter based on the type T. // // Default converters are available for the following types: // - string // - int, int8, int16, int32, int64 // - uint, uint8, uint16, uint32, uint64, uintptr // - float32, float64 // - complex64, complex128 // - bool // // If no default converter is available, a custom converter must be provided otherwise the environment variable value cannot be retrieved. Convert(converter Converter[T]) TypedVariable[T] // Value retrieves the environment variable value. // // If the environment variable is not set or an error occurs, it either returns preconditioned values and an error or panics if the die flag is set. Value() (T, error) // Raw retrieves the environment variable value as a string without converting it to type T. // // If the environment variable is not set or an error occurs, it returns the zero value of type T and the error. Raw() (string, error) }
TypedVariable represents a typed environment variable.
type Variable ¶
type Variable[T any] interface { // OrDie sets the die flag to panic if the environment variable is not set or an error occurs. OrDie() RequiredVariable[T] // WithFallback sets the default value to be used if the environment variable is not set or an error occurs. WithFallback(defaultValue T) OptionalVariable[T] // NoFallback flags the environment variable builder to not use a fallback value. // If the environment variable is not set or an error occurs, it returns the zero value of type T and the error. NoFallback() RequiredVariable[T] }
Variable represents a builder for an environment variable of type T.
The variable can be required or optional. Required variables must be set, while optional variables have a fallback value.
func Get ¶
Get returns a Variable for the provided key. You can then use the builder to set fallback values, converters, and more.
The type T must be a valid type for an environment variable. Panics if T is an invalid type for an environment variable.
Example:
// Get the environment variable "MY_VAR" as a string and panic if an error occurs. value, _ := env.Get[string]("MY_VAR").OrDie().Value() // Get the environment variable "MY_VAR" as an integer. // If the variable is not set or an error occurs, return 42. value, _ := env.Get[int]("MY_VAR").WithFallback(42).Value() // Get the environment variable "MY_VAR" as a [time.Duration] using a custom converter. // If the variable is not set or an error occurs, return 5 * time.Second. value, _ := env.Get[time.Duration]("MY_VAR").WithFallback(5*time.Second).Convert(time.ParseDuration).Value() // Get the environment variable "MY_VAR" as a boolean. // If the variable is not set or an error occurs, return the error. value, err := env.Get[bool]("MY_VAR").NoFallback().Value()