Skip to content

Go high level wrapper around godotenv

License

Notifications You must be signed in to change notification settings

dsh2dsh/expx-dotenv

Repository files navigation

Go high level wrapper around godotenv

Go Go Reference

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.

Instalation

go get https://github.com/dsh2dsh/expx-dotenv

Usage

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)

About

Go high level wrapper around godotenv

Resources

License

Stars

Watchers

Forks

Packages

No packages published