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
go get github.com/SbstnErhrdt/env
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.
Loads different env files. Prints warnings if the files not present.
...
env.LoadEnvFiles(filenames ...string)
...
Specify filename:
...
env.LoadEnvFiles("production.enbv")
...
Sets fallback variables for env variables.
...
env.FallbackEnvVariable("environmentVariableKey", "fallbackValue")
...
Checks if there are required environment variables not set.
...
env.CheckRequiredEnvironmentVariables("environmentVariableKey0", "environmentVariableKey1")
...
Checks if there are optional environment variables not set.
...
env.CheckOptionalEnvironmentVariables("environmentVariableKey0", "environmentVariableKey1")
...
Checks if there are optional environment variables not set.
...
varName := env.RequiredEnvVariable("environmentVariableKey0")
...