typenv
is a minimalistic, zero-dependency Go library for typed environment variables.
It eliminates the need for manual type conversion and allows default values for missing environment variables.
var _ time.Duration = typenv.Duration("MY_ENV")
var _ string = typenv.String("MY_ENV")
var _ float64 = typenv.Float64("MY_ENV")
var _ float32 = typenv.Float32("MY_ENV")
var _ int64 = typenv.Int64("MY_ENV")
var _ int32 = typenv.Int32("MY_ENV")
var _ int16 = typenv.Int16("MY_ENV")
var _ int8 = typenv.Int8("MY_ENV")
var _ int = typenv.Int("MY_ENV")
var _ bool = typenv.Bool("MY_ENV")
And their slice variants:
var _ []time.Duration = typenv.Slice[[]time.Duration]("MY_ENV", ",")
var _ []string = typenv.Slice[[]string]("MY_ENV", ",")
var _ []float64 = typenv.Slice[[]float64]("MY_ENV", ",")
var _ []float32 = typenv.Slice[[]float32]("MY_ENV", ",")
var _ []int64 = typenv.Slice[[]int64]("MY_ENV", ",")
var _ []int32 = typenv.Slice[[]int32]("MY_ENV", ",")
var _ []int16 = typenv.Slice[[]int16]("MY_ENV", ",")
var _ []int8 = typenv.Slice[[]int8]("MY_ENV", ",")
var _ []int = typenv.Slice[[]int]("MY_ENV", ",")
var _ []bool = typenv.Slice[[]bool]("MY_ENV", ",")
- ✅ Eliminates manual type conversions
- ✅ Support slices out of the box
- ✅ Allows fallback value for missing environment variables
- ✅ Zero external dependencies
Go get the library:
go get diegomarangoni.dev/typenv
And use it:
if typenv.Bool("DEBUG") {
// do something
}
If the environment is NOT set, the zero value of the type will return, in this case false
.
You can fallback to a default value in case the environment variable is not set.
if typenv.Bool("DEBUG", true) {
// do something
}
If the environment is NOT set, the return value will be true
.