Documentation
¶
Overview ¶
Package env provides utilities for reading environment variables with type conversion and fallback values.
All functions return a fallback value if the environment variable is not set or cannot be parsed to the requested type.
Usage ¶
port := env.GetAsInt("PORT", 8080)
debug := env.GetAsBool("DEBUG", false)
hosts := env.GetAsStringArr("HOSTS", "localhost")
Index ¶
- func Get(key, fallback string) string
- func GetAsBool(key string, fallback bool) bool
- func GetAsBoolArr(key string, fallback string) []bool
- func GetAsFloat64(key string, fallback float64) float64
- func GetAsFloat64Arr(key string, fallback string) []float64
- func GetAsInt(key string, fallback int) int
- func GetAsIntArr(key string, fallback string) []int
- func GetAsStringArr(key, fallback string) []string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
Get returns the value of the environment variable with the given key, or the fallback value if the key is not set
Parameters:
- key: The key of the environment variable
- fallback: The fallback value if the key is not set
Returns:
- string: The value of the environment variable
func GetAsBool ¶
GetAsBool returns the value of the environment variable with the given key, or the fallback value if the key is not set
Parameters:
- key: The key of the environment variable
- fallback: The fallback value if the key is not set
Returns:
- bool: The value of the environment variable
func GetAsBoolArr ¶
GetAsBoolArr returns the value of the environment variable as a boolean slice. Values are expected to be comma-separated booleans. Invalid booleans are skipped.
Example:
// FEATURES=true,false,true
features := env.GetAsBoolArr("FEATURES", "false")
func GetAsFloat64 ¶
GetAsFloat64 returns the value of the environment variable as a float64. Returns the fallback value if the variable is not set or cannot be parsed.
Example:
// TIMEOUT=30.5
timeout := env.GetAsFloat64("TIMEOUT", 10.0)
func GetAsFloat64Arr ¶
GetAsFloat64Arr returns the value of the environment variable as a float64 slice. Values are expected to be comma-separated floats. Invalid floats are skipped.
Example:
// WEIGHTS=0.5,0.3,0.2
weights := env.GetAsFloat64Arr("WEIGHTS", "1.0")
func GetAsInt ¶
GetAsInt returns the value of the environment variable with the given key, or the fallback value if the key is not set
Parameters:
- key: The key of the environment variable
- fallback: The fallback value if the key is not set
Returns:
- int: The value of the environment variable
func GetAsIntArr ¶
GetAsIntArr returns the value of the environment variable as an integer slice. Values are expected to be comma-separated integers. Invalid integers are skipped.
Example:
// PORTS=8080,8081,8082
ports := env.GetAsIntArr("PORTS", "8080")
func GetAsStringArr ¶
GetAsStringArr returns the value of the environment variable as a string slice. Values are expected to be comma-separated. Returns the fallback split by commas if the environment variable is not set.
Example:
// HOSTS=host1,host2,host3
hosts := env.GetAsStringArr("HOSTS", "localhost")
Types ¶
This section is empty.