Skip to content

Commit adadc05

Browse files
committed
Modes in environment variable
1 parent d6ee687 commit adadc05

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

README.md

+21-10
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,31 @@ Existing envs take precendence of envs that are loaded later.
116116

117117
The [convention](https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use)
118118
for managing multiple environments (i.e. development, test, production)
119-
is to create an env named `{YOURAPP}_ENV` and load envs in this order:
119+
is to create an env named `APP_ENV` and load envs in this order:
120120

121121
```go
122-
env := os.Getenv("FOO_ENV")
123-
if "" == env {
124-
env = "development"
122+
env := os.Getenv("APP_ENV")
123+
if env == "" {
124+
env = "development"
125125
}
126126

127-
godotenv.Load(".env." + env + ".local")
128-
if "test" != env {
129-
godotenv.Load(".env.local")
130-
}
131-
godotenv.Load(".env." + env)
132-
godotenv.Load() // The Original .env
127+
base := ".env" // .env
128+
local := base + ".local" // .env.local
129+
mode := base + "." + env // .env.[mode]
130+
localM := mode + ".local" // .env.[mode].local
131+
132+
godotenv.Load(localM)
133+
godotenv.Load(mode)
134+
godotenv.Load(local)
135+
godotenv.Load(base)
136+
```
137+
138+
Sometimes you might have env variables that should not be committed into the codebase, especially if your project is hosted in a public repository. In this case you must add the following snippet in your `.gitignore` file:
139+
140+
```gitignore
141+
# local env files
142+
.env.local
143+
.env.*.local
133144
```
134145

135146
If you need to, you can also use `godotenv.Overload()` to defy this convention

0 commit comments

Comments
 (0)