Documentation
¶
Overview ¶
Package getenv provides functionality for loading environment variables.
Index ¶
- func BoolOrDefault(key string, defaultVal bool) booldeprecated
- func DurationOrDefault(key string, defaultVal time.Duration) time.Durationdeprecated
- func EnvOrDefault[T internal.EnvParsable](key string, defaultVal T, options ...option.Option) T
- func Float64OrDefault(key string, defaultVal float64) float64deprecated
- func Float64SliceOrDefault(key string, defaultVal []float64, sep string) []float64deprecated
- func Int64OrDefault(key string, defaultVal int64) int64deprecated
- func Int64SliceOrDefault(key string, defaultVal []int64, sep string) []int64deprecated
- func IntOrDefault(key string, defaultVal int) intdeprecated
- func IntSliceOrDefault(key string, defaultVal []int, sep string) []intdeprecated
- func StringOrDefault(key, defaultVal string) stringdeprecated
- func StringSliceOrDefault(key string, defaultVal []string, sep string) []stringdeprecated
- func TimeOrDefault(key string, defaultVal time.Time, layout string) time.Timedeprecated
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BoolOrDefault
deprecated
func DurationOrDefault
deprecated
func EnvOrDefault ¶ added in v1.2.0
func EnvOrDefault[T internal.EnvParsable](key string, defaultVal T, options ...option.Option) T
EnvOrDefault retrieves the value of the environment variable named by the key. If variable not set or value is empty - defaultVal will be returned.
Example ¶
key := "GH_GETENV_TEST"
defer func() {
if err := os.Unsetenv("GH_GETENV_TEST"); err != nil {
panic(err)
}
}()
var val any
// string
if err := os.Setenv(key, "golly"); err != nil {
panic(err)
}
val = EnvOrDefault(key, "golly")
fmt.Printf("[%T]: %v\n", val, val)
// int
if err := os.Setenv(key, "123"); err != nil {
panic(err)
}
val = EnvOrDefault(key, -99)
fmt.Printf("[%T]: %v\n", val, val)
// time.Time
if err := os.Setenv(key, "2022-01-20"); err != nil {
panic(err)
}
val = EnvOrDefault(key,
time.Date(1992, 12, 1, 0, 0, 0, 0, time.UTC),
option.WithTimeLayout("2006-01-02"),
)
fmt.Printf("[%T]: %v\n", val, val)
// []float64
if err := os.Setenv(key, "26.89,0.67"); err != nil {
panic(err)
}
val = EnvOrDefault(key, []float64{-99},
option.WithSeparator(","),
)
fmt.Printf("[%T]: %v\n", val, val)
// time.Duration
if err := os.Setenv(key, "2h35m"); err != nil {
panic(err)
}
val = EnvOrDefault(key, time.Second)
fmt.Printf("[%T]: %v\n", val, val)
Output: [string]: golly [int]: 123 [time.Time]: 2022-01-20 00:00:00 +0000 UTC [[]float64]: [26.89 0.67] [time.Duration]: 2h35m0s
func Float64OrDefault
deprecated
func Float64SliceOrDefault
deprecated
added in
v1.2.0
Float64SliceOrDefault retrieves the float64 slice value of the environment variable named by the key and separated by sep. If variable not set or value is empty - defaultVal will be returned.
Deprecated: use EnvOrDefault.
func Int64OrDefault
deprecated
func Int64SliceOrDefault
deprecated
added in
v1.2.0
func IntOrDefault
deprecated
func IntSliceOrDefault
deprecated
added in
v1.2.0
func StringOrDefault
deprecated
func StringSliceOrDefault
deprecated
func TimeOrDefault
deprecated
Types ¶
This section is empty.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package internal provides internal implementation logic for environment variables parsing.
|
Package internal provides internal implementation logic for environment variables parsing. |
|
Package option provides options for parsing environment variables.
|
Package option provides options for parsing environment variables. |
Click to show internal directories.
Click to hide internal directories.