Skip to content

Latest commit

 

History

History
45 lines (39 loc) · 1.33 KB

README.md

File metadata and controls

45 lines (39 loc) · 1.33 KB

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)