Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sbstnerhrdt/env

Environment variable handling in go
https://github.com/sbstnerhrdt/env

env environment environment-variables go golang

Last synced: about 1 month ago
JSON representation

Environment variable handling in go

Awesome Lists containing this project

README

        

# 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:

```go
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.

```go
...
env.LoadEnvFiles(filenames ...string)
...
```

Specify filename:

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

### Fallback environment variables

Sets fallback variables for env variables.

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

### Required variables check

Checks if there are **required** environment variables not set.

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

### Optional variables check

Checks if there are optional environment variables not set.

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

### Required variable check and return

Checks if there are optional environment variables not set.

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