typenv

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2025 License: Unlicense Imports: 5 Imported by: 1

README

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?

Handling environment variables with types other than strings can be cumbersome. typenv simplifies this process by:

  • ✅ Eliminating manual type conversions
  • ✅ Supporting slices out of the box
  • ✅ Allowing fallback values for missing environment variables
  • ✅ Having 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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(name string, fallback ...bool) bool

Bool returns given environment variable as boolean. Returns `true` for if any of options: y, yes, true, 1; Returns `false` for anything else.

func Duration

func Duration(name string, fallback ...time.Duration) time.Duration

Duration returns given environment variable as time.Duration

func E deprecated

func E(fn any, name string, val any) any

E was used to set a global default for a given environment variable.

Deprecated: Global defaults is a bad idea

func Float32

func Float32(name string, fallback ...float32) float32

Float32 returns given environment variable as float32

func Float64

func Float64(name string, fallback ...float64) float64

Float64 returns given environment variable as float64

func Int

func Int(name string, fallback ...int) int

Int returns given environment variable as int

func Int16 added in v1.1.0

func Int16(name string, fallback ...int16) int16

Int16 returns given environment variable as int16

func Int32

func Int32(name string, fallback ...int32) int32

Int32 returns given environment variable as int32

func Int64

func Int64(name string, fallback ...int64) int64

Int64 returns given environment variable as int64

func Int8

func Int8(name string, fallback ...int8) int8

Int8 returns given environment variable as int8

func SetGlobalDefault deprecated

func SetGlobalDefault(...any)

SetGlobalDefault was used to set a global default for a given environment variable.

Deprecated: Global defaults is a bad idea

func Slice added in v1.2.0

func Slice[T slices](name string, separator string, fallback ...T) T

Slice returns given environment variable as a slice

func String

func String(name string, fallback ...string) string

String returns given environment variable as string

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL