- .env File Support: Load variables from a
.env
file and/or any other file(s) - Type Conversions: Easily convert environment variables to Go types
- Validation: Check common configuration errors (e.g.
as.PortNo
to enforce 0 <= X <= 65535) - Testing: Convenient testing utilities
go get github.com/blugnu/env
Demonstrates the use of the env.Override
function to replace a default
configuration value with a value parsed from an environment variable:
port := 8080
if _, err := env.Override(&port, "SERVICE_PORT", as.PortNo); err != nil {
log.Fatal(err)
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
Demonstrates the use of the env.Parse
function to parse a required
configuration value from an environment variable:
authURL, err := env.Parse("AUTH_SERVICE_URL", as.AbsoluteURL)
if err != nil {
log.Fatal(err)
}
Demonstrates the use of the env.Load
function to load configuration:
by default, with no filename(s) specified, the
Load()
function loads configuration from a.env
file.
if err := env.Load(); err != nil {
log.Fatal(err)
}
Demonstrates the use of defer env.State().Reset()
to preserve environment
variables during a test:
func TestSomething(t *testing.T) {
// ARRANGE
defer env.State().Reset()
env.Vars{
"SOME_VAR": "some value",
"ANOTHER_VAR": "another value",
}.Set()
// ACT
SomeFuncUsingEnvVars()
// ASSERT
...
}
Contributions are welcome! Please feel free to submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.