8000 GitHub - diegomarangoni/typenv: Go minimalist typed environment variables library
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Go minimalist typed environment variables library

License

Notifications You must be signed in to change notification settings

diegomarangoni/typenv

Repository files navigation

Typenv

go.dev go report codecov

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.

Supported Types

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", ",")

Why typenv?

  • ✅ Eliminates manual type conversions
  • ✅ Support slices out of the box
  • ✅ Allows fallback value for missing environment variables
  • ✅ Zero external dependencies

How to use it

Basic usage

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.

With default value

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.

About

Go minimalist typed environment variables library

Topics

Resources

License

Stars

Watchers

Forks

Languages

0