Documentation
¶
Overview ¶
Package env provides utilities for reading environment variables with type conversion and default value support. It handles various Go types including strings, integers (signed and unsigned), floats, booleans, durations, and their slice variants.
All Get functions follow a consistent pattern: they attempt to read the specified environment variable, parse it to the appropriate type, and return the default value if the variable is not set or cannot be parsed.
Index ¶
- func GetBase64ToBytes(key string, defaultValue []byte) []byte
- func GetBase64ToString(key string, defaultValue string) string
- func GetBool(key string, defaultValue bool) bool
- func GetBoolSlice(key, sep string, defaultValue []bool) []bool
- func GetBytes(key string, defaultValue []byte) []byte
- func GetDuration(key string, defaultValue int64, duration time.Duration) time.Duration
- func GetFloat32(key string, defaultValue float32) float32
- func GetFloat32Slice(key, sep string, defaultValue []float32) []float32
- func GetFloat64(key string, defaultValue float64) float64
- func GetFloat64Slice(key, sep string, defaultValue []float64) []float64
- func GetInt(key string, defaultValue int) int
- func GetInt16(key string, defaultValue int16) int16
- func GetInt16Slice(key, sep string, defaultValue []int16) []int16
- func GetInt32(key string, defaultValue int32) int32
- func GetInt32Slice(key, sep string, defaultValue []int32) []int32
- func GetInt64(key string, defaultValue int64) int64
- func GetInt64Slice(key, sep string, defaultValue []int64) []int64
- func GetInt8(key string, defaultValue int8) int8
- func GetInt8Slice(key, sep string, defaultValue []int8) []int8
- func GetIntSlice(key, sep string, defaultValue []int) []int
- func GetString(key, defaultValue string) string
- func GetStringSlice(key, sep string, defaultValue []string) []string
- func GetUint(key string, defaultValue uint) uint
- func GetUint16(key string, defaultValue uint16) uint16
- func GetUint16Slice(key, sep string, defaultValue []uint16) []uint16
- func GetUint32(key string, defaultValue uint32) uint32
- func GetUint32Slice(key, sep string, defaultValue []uint32) []uint32
- func GetUint64(key string, defaultValue uint64) uint64
- func GetUint64Slice(key, sep string, defaultValue []uint64) []uint64
- func GetUint8(key string, defaultValue uint8) uint8
- func GetUint8Slice(key, sep string, defaultValue []uint8) []uint8
- func GetUintSlice(key, sep string, defaultValue []uint) []uint
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetBase64ToBytes ¶ added in v0.5.0
GetBase64ToBytes retrieves a base64-encoded string from the environment variable specified by key and decodes it to a byte slice. If the environment variable is not set or the value cannot be decoded as base64, it returns defaultValue.
func GetBase64ToString ¶ added in v0.5.0
GetBase64ToString retrieves a base64-encoded string from the environment variable specified by key and decodes it to a string. If the environment variable is not set or the value cannot be decoded as base64, it returns defaultValue.
func GetBool ¶
GetBool retrieves a bool value from the environment variable specified by key. If the environment variable is not set or cannot be parsed as a boolean, it returns defaultValue. Accepted boolean values are defined by strconv.ParseBool (1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False).
func GetBoolSlice ¶ added in v0.2.0
GetBoolSlice retrieves a bool slice from the environment variable specified by key, splitting the value using sep as the separator and parsing each element as a boolean. If the environment variable is not set or any element cannot be parsed, it returns defaultValue.
func GetBytes ¶
GetBytes retrieves a byte slice from the environment variable specified by key, converting the string value to []byte. If the environment variable is not set, it returns defaultValue.
func GetDuration ¶ added in v0.3.0
GetDuration retrieves an int64 value from the environment variable specified by key and converts it to a time.Duration by multiplying with the provided duration unit. For example, to read seconds: GetDuration("TIMEOUT", 30, time.Second). If the environment variable is not set or cannot be parsed, it uses defaultValue.
func GetFloat32 ¶
GetFloat32 retrieves a float32 value from the environment variable specified by key. If the environment variable is not set or cannot be parsed as a float32, it returns defaultValue.
func GetFloat32Slice ¶ added in v0.2.0
GetFloat32Slice retrieves a float32 slice from the environment variable specified by key, splitting the value using sep as the separator and parsing each element as a float32. If the environment variable is not set or any element cannot be parsed, it returns defaultValue.
func GetFloat64 ¶
GetFloat64 retrieves a float64 value from the environment variable specified by key. If the environment variable is not set or cannot be parsed as a float64, it returns defaultValue.
func GetFloat64Slice ¶ added in v0.2.0
GetFloat64Slice retrieves a float64 slice from the environment variable specified by key, splitting the value using sep as the separator and parsing each element as a float64. If the environment variable is not set or any element cannot be parsed, it returns defaultValue.
func GetInt ¶
GetInt retrieves an int value from the environment variable specified by key. If the environment variable is not set or cannot be parsed as an integer, it returns defaultValue.
func GetInt16 ¶ added in v0.5.0
GetInt16 retrieves an int16 value from the environment variable specified by key. If the environment variable is not set or cannot be parsed as an int16, it returns defaultValue.
func GetInt16Slice ¶ added in v0.5.0
GetInt16Slice retrieves an int16 slice from the environment variable specified by key, splitting the value using sep as the separator and parsing each element as an int16. If the environment variable is not set or any element cannot be parsed, it returns defaultValue.
func GetInt32 ¶
GetInt32 retrieves an int32 value from the environment variable specified by key. If the environment variable is not set or cannot be parsed as an int32, it returns defaultValue.
func GetInt32Slice ¶ added in v0.2.0
GetInt32Slice retrieves an int32 slice from the environment variable specified by key, splitting the value using sep as the separator and parsing each element as an int32. If the environment variable is not set or any element cannot be parsed, it returns defaultValue.
func GetInt64 ¶
GetInt64 retrieves an int64 value from the environment variable specified by key. If the environment variable is not set or cannot be parsed as an int64, it returns defaultValue.
func GetInt64Slice ¶ added in v0.2.0
GetInt64Slice retrieves an int64 slice from the environment variable specified by key, splitting the value using sep as the separator and parsing each element as an int64. If the environment variable is not set or any element cannot be parsed, it returns defaultValue.
func GetInt8 ¶ added in v0.5.0
GetInt8 retrieves an int8 value from the environment variable specified by key. If the environment variable is not set or cannot be parsed as an int8, it returns defaultValue.
func GetInt8Slice ¶ added in v0.5.0
GetInt8Slice retrieves an int8 slice from the environment variable specified by key, splitting the value using sep as the separator and parsing each element as an int8. If the environment variable is not set or any element cannot be parsed, it returns defaultValue.
func GetIntSlice ¶ added in v0.2.0
GetIntSlice retrieves an int slice from the environment variable specified by key, splitting the value using sep as the separator and parsing each element as an integer. If the environment variable is not set or any element cannot be parsed, it returns defaultValue.
func GetString ¶
GetString retrieves a string value from the environment variable specified by key. If the environment variable is not set, it returns defaultValue.
func GetStringSlice ¶ added in v0.2.0
GetStringSlice retrieves a string slice from the environment variable specified by key, splitting the value using sep as the separator. If the environment variable is not set, it returns defaultValue.
func GetUint ¶ added in v0.4.0
GetUint retrieves a uint value from the environment variable specified by key. If the environment variable is not set or cannot be parsed as a uint, it returns defaultValue.
func GetUint16 ¶ added in v0.5.0
GetUint16 retrieves a uint16 value from the environment variable specified by key. If the environment variable is not set or cannot be parsed as a uint16, it returns defaultValue.
func GetUint16Slice ¶ added in v0.5.0
GetUint16Slice retrieves a uint16 slice from the environment variable specified by key, splitting the value using sep as the separator and parsing each element as a uint16. If the environment variable is not set or any element cannot be parsed, it returns defaultValue.
func GetUint32 ¶ added in v0.4.0
GetUint32 retrieves a uint32 value from the environment variable specified by key. If the environment variable is not set or cannot be parsed as a uint32, it returns defaultValue.
func GetUint32Slice ¶ added in v0.4.0
GetUint32Slice retrieves a uint32 slice from the environment variable specified by key, splitting the value using sep as the separator and parsing each element as a uint32. If the environment variable is not set or any element cannot be parsed, it returns defaultValue.
func GetUint64 ¶ added in v0.4.0
GetUint64 retrieves a uint64 value from the environment variable specified by key. If the environment variable is not set or cannot be parsed as a uint64, it returns defaultValue.
func GetUint64Slice ¶ added in v0.4.0
GetUint64Slice retrieves a uint64 slice from the environment variable specified by key, splitting the value using sep as the separator and parsing each element as a uint64. If the environment variable is not set or any element cannot be parsed, it returns defaultValue.
func GetUint8 ¶ added in v0.5.0
GetUint8 retrieves a uint8 value from the environment variable specified by key. If the environment variable is not set or cannot be parsed as a uint8, it returns defaultValue.
func GetUint8Slice ¶ added in v0.5.0
GetUint8Slice retrieves a uint8 slice from the environment variable specified by key, splitting the value using sep as the separator and parsing each element as a uint8. If the environment variable is not set or any element cannot be parsed, it returns defaultValue.
func GetUintSlice ¶ added in v0.4.0
GetUintSlice retrieves a uint slice from the environment variable specified by key, splitting the value using sep as the separator and parsing each element as a uint. If the environment variable is not set or any element cannot be parsed, it returns defaultValue.
Types ¶
This section is empty.