/env

Environment variable handling in go

Primary LanguageGoOtherNOASSERTION

Env

This is a small go module that helps with environment variables.

  • Load environment files .env
  • Provides fallback variables
  • uses slog
  • no dependencies

Based on https://github.com/joho/godotenv

Installation

go get github.com/SbstnErhrdt/env

Usage

Put a .env file in the working directory. This might look like this

KEY=value1234

In your main() funcion:

func main() {
// load env
env.LoadEnvFiles() // reads the .env file in the working directory
...
}

If no filename is provided, the file .env in the working directory is used.

Load env files

Loads different env files. Prints warnings if the files not present.

...
env.LoadEnvFiles(filenames ...string)
...

Specify filename:

...
env.LoadEnvFiles("production.enbv")
...

Fallback environment variables

Sets fallback variables for env variables.

...
env.FallbackEnvVariable("environmentVariableKey", "fallbackValue")
...

Required variables check

Checks if there are required environment variables not set.

...
env.CheckRequiredEnvironmentVariables("environmentVariableKey0", "environmentVariableKey1")
...

Optional variables check

Checks if there are optional environment variables not set.

...
env.CheckOptionalEnvironmentVariables("environmentVariableKey0", "environmentVariableKey1")
...

Required variable check and return

Checks if there are optional environment variables not set.

...
varName := env.RequiredEnvVariable("environmentVariableKey0")
...