ev

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2025 License: MIT Imports: 4 Imported by: 0

README

Ev

E(nvironment) v(ariable)

Tiny Go library to create typed environment variables.

go get github.com/metafates/ev

Example

const MyEnvVar ev.Var[int] = "MY_ENV_VAR"

const Verbose ev.Var[bool] = "VERBOSE"

func main() {
    // assume we have the following variables set
    os.Setenv("MY_ENV_VAR", "42")
    os.Setenv("VERBOSE", "true")

    if Verbose.Get() {
        n := MyEnvVar.Get()

        fmt.Println(n + n) // prints 84
    }
}

Supported types:

type Constraint interface {
    int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64
}

Values are parsed using fmt.Sscanf.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Or

func Or[T Constraint](vars ...Var[T]) T

Or returns the first of its arguments for which Var.TryGet returns ok == true. If no argument is suitable, it returns the zero value.

func SetDefault

func SetDefault[T Constraint](v Var[T], value T)

SetDefault sets default value for the given var.

This function is safe to call from multiple goroutines.

Types

type Constraint

type Constraint interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64
}

Constraint defines all supported types for Var.

It does not permit ~T (tilde) types since other types may be parsed differently, which may result unexpected behavior.

type Var

type Var[T Constraint] string

Var is an environment variable.

T defines which value type this is expected by this variable.

func (Var[T]) Get

func (v Var[T]) Get() T

Get returns the value of this variable. It value is missing or not scannable by fmt.Sscanf it returns default value.

See also SetDefault to set a default value.

func (Var[T]) GetOr

func (v Var[T]) GetOr(or ...T) T

GetOr returns the first of its arguments that is not equal to the zero value. If no argument is non-zero, it returns the zero value.

func (Var[T]) TryGet

func (v Var[T]) TryGet() (T, bool)

TryGet returns a value of this variable and boolean stating whether value was present and fmt.Sscanf successfully scanned it.

Jump to

Keyboard shortcuts

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