Enver is a Go package designed to simplify the process of reading environment variables with default values from various sources. It provides a straightforward API that allows developers to easily integrate environment variable management into their applications. Enver supports reading from two primary sources:
- Environment Variables: Directly from the system's environment variables.
- .env Files: From
.env
files located within your project directory.
Additionally, Enver enables overriding values by specifying multiple sources, with precedence given to the sources listed first.
- Read environment variables directly from the system or
.env
files. - Specify default values for environment variables.
- Override environment variable values by specifying multiple sources with precedence.
To use Enver in your Go project, you need to install it by running:
go get github.com/msrexe/enver
Ensure you have Go 1.18 or higher, as specified in the go.mod
file.
Define a struct with fields representing your configuration parameters. Use struct tags to specify the environment variable keys and default values:
type Config struct {
Port string `env:"PORT,8080"`
Host string `env:"HOST,localhost"`
}
To read environment variables into your struct, use the Fill
function provided by Enver. You can specify one or more sources (Source_Env
for system environment variables and Source_DotEnv
for .env
file variables):
config := Config{}
err := enver.Fill(&config, &enver.Source_Env{}, &enver.Source_DotEnv{})
if err != nil {
log.Fatalf("Error filling config: %v", err)
}
If you need to specify a custom path for your .env
file, you can do so by setting the Path
field of Source_DotEnv
:
dotenvSource := enver.Source_DotEnv{Path: "path/to/your/.env"}
err := enver.Fill(&config, &dotenvSource)
Enver includes a set of unit tests demonstrating how to test reading from environment variables and .env
files. Refer to the enver_test.go
file for examples.
Contributions to Enver are welcome! Please feel free to submit issues, pull requests, or enhancements to improve the library.
Enver is open-source software licensed under the MIT license.