Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for fallback if env variable is missing #111

Open
lesichkovm opened this issue Jul 1, 2020 · 4 comments
Open

Add support for fallback if env variable is missing #111

lesichkovm opened this issue Jul 1, 2020 · 4 comments

Comments

@lesichkovm
Copy link

Would be nice if there was option for fallback option if a variable is missing

Something similar to:

func env(key, fallback string) string {
value, exists := os.LookupEnv(key)
if !exists {
value = fallback
}
return value
}

@lesichkovm lesichkovm changed the title Add support for fallback if env variable, of missing Add support for fallback if env variable is missing Jul 1, 2020
@D-sense
Copy link

D-sense commented Jul 3, 2020

@lesichkovm, I think there is a similar logic provided already. When you call the Load(filenames ...string) or Overload(filenames ...string), it first calls on filenamesOrDefault(filenames); If "filenames" is empty, a fallback is provided. This fallback expects to find ".env" file in the root folder.

@lesichkovm
Copy link
Author

@D-sense it is not about loading files, but a helper function to check if an environmental variable exists:

myVar := Env("MYENV", "DEFAULT_IF_NOT_SET")

@D-sense
Copy link

D-sense commented Jul 5, 2020

@lesichkovm , please ignore the tagging of the PR. It was a mistake (I didn’t mean to tag it here).

On the point you raised, except I still haven’t gotten your point well enough, I think the existing logic already caters for that need (as outlined in my earlier response). I might be wrong though.

@nerg4l
Copy link

nerg4l commented Oct 2, 2020

If I understand @lesichkovm correctly he would like to have a function which simplifies the following:

val, ok := os.LookupEnv("KEY")
if !ok {
  val = "DEFAULT_VAL"
}

Instead of the above code you could write this:

val := godotenv.Env("KEY", "DEFAULT_VAL")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants