Documentation
¶
Overview ¶
package envconv provides utilities for converting environment variable values
Index ¶
- func GetBool(name string, defaultValue bool) bool
- func GetDuration(name string, defaultValue time.Duration) time.Duration
- func GetInt(name string, defaultValue int) int
- func GetSlogLevel(name string, defaultValue slog.Level) slog.Leveldeprecated
- func GetString(name, defaultValue string) string
- func GetTextUnmarshaler[T any, TPtr interface{ ... }](name string, defaultValue T) T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetBool ¶
GetBool retrieves the boolean value of the environment variable named by the key.
The conversion follows strconv.ParseBool rules. If the variable is not set or cannot be converted to a boolean, it returns the provided defaultValue.
func GetDuration ¶
GetDuration retrieves the time.Duration value of the environment variable named by the key.
The conversion follows time.ParseDuration rules. If the variable is not set or cannot be converted to a time.Duration, it returns the provided defaultValue.
func GetInt ¶
GetInt retrieves the integer value of the environment variable named by the key.
The conversion follows strconv.Atoi rules. If the variable is not set or cannot be converted to an integer, it returns the provided defaultValue.
func GetSlogLevel
deprecated
GetSlogLevel retrieves the slog.Level value of the environment variable named by the key.
The conversion follows slog.Level.UnmarshalText rules. If the variable is not set or cannot be converted to a slog.Level, it returns the provided defaultValue.
Deprecated: use GetTextUnmarshaler instead.
func GetString ¶
GetString retrieves the string value of the environment variable named by the key.
If the variable is not set, it returns the provided defaultValue.
func GetTextUnmarshaler ¶ added in v0.2.0
func GetTextUnmarshaler[T any, TPtr interface { *T encoding.TextUnmarshaler }](name string, defaultValue T) T
GetTextUnmarshaler retrieves a value of type T that implements encoding.TextUnmarshaler from the environment variable named by the key.
If the variable is not set or cannot be converted, it returns the provided defaultValue. The type TPtr is a pointer to T that implements encoding.TextUnmarshaler.
Example usage:
var slogLevel slog.Level = envconv.GetTextUnmarshaler("MY_LOG_LEVEL", slog.LevelInfo)
var netIP net.IP = envconv.GetTextUnmarshaler("MY_IP", net.IPv4(192.168, 0, 1))
Types ¶
This section is empty.