Skip to content

code-gorilla-au/env

Repository files navigation

env

simple env vars, checks if the env var exists or returns the zero value

credit goes to https://github.com/17twenty for initial authoring

Go Report Card Go Reference

Basic use

 env := env.GetAsString("ENV", "dev")

 isFlagEnabled := env.GetAsBool("FEATURE_ONE")

 allowList := env.GetAsSlice("ALLOW_LIST", ",")

Load env file

LoadEnvFile("./.env.local")

foo := env.GetAsString("ENV", "dev")

Strict mode

env.WithStrictMode()

// ENV goes not exist
// panics
foo := env.GetAsString("ENV")

With defaults

env.WithStrictMode()

// ENV goes not exist, returns bar
foo := env.GetAsStringWithDefault("ENV", "bar")
env.WithStrictMode()

// ENV goes not exist, returns 1
foo := env.GetAsIntWithDefault("ENV", 1)