Go high level wrapper around godotenv
It allows to load one or multiple .env file(s) according to original rules. It searches for .env file(s) in current and parent dirs, until it find at least one of them.
go get https://github.com/dsh2dsh/expx-dotenv
env := dotenv.New()
if err := env.Load(); err != nil {
log.Fatalf("error loading .env files: %v", err)
}
or with chained calls:
env := dotenv.New()
if err := env.WithDepth(1).WithEnvSuffix("test").Load(); err != nil {
log.Fatalf("error loading .env files: %v", err)
}
Load environment variables and parse them into a struct:
env := dotenv.New()
cfg := struct {
SomeOpt string `env:"ENV_VAR1"`
}{
SomeOpt: "some default value, because we don't have .env file(s)",
}
if err := env.LoadTo(&cfg); err != nil {
log.Fatalf("error loading .env files: %v", err)
}
fmt.Println(cfg.SomeOpt)
// Output: some default value, because we don't have .env file(s)