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

proposal: os: required env variables with MustGetenv #60339

Closed
igoracmelo opened this issue May 22, 2023 · 2 comments
Closed

proposal: os: required env variables with MustGetenv #60339

igoracmelo opened this issue May 22, 2023 · 2 comments
Labels
Milestone

Comments

@igoracmelo
Copy link

In many application we use environment variables for things such as API keys, database connection credentials, and so on.

Many times we wish to get an environment variable and ensure it is not empty, otherwise the application immediately, but since we can't check that outside a function, we do something like this:

var dbURL = os.Getenv("DB_URL")

func init() {
  if dbURL == "" {
    panic("missing env variable DB_URL")
  }
}

Since many applications can't run without some env variables and would rather prefer to "fail first", sounds reasonable to me a function like this:

var dbURL = os.MustGetenv("DB_URL")

func main() {
  // at this point I know for sure that dbURL is filled
}
// implementation example
package os

func MustGetenv(key string) string {
  val := os.Getenv(key)
  if val == "" {
    panic(fmt.Sprintf("missing environment variable '%s'", key))
  }
  return val
}

I know it is something trivial to implement by your own in your projects, but Go stdlib has many functions that are trivial to implement but very useful at the same time.

@gopherbot gopherbot added this to the Proposal milestone May 22, 2023
@seankhliao
Copy link
Member

Duplicate of #54297

@seankhliao seankhliao marked this as a duplicate of #54297 May 22, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale May 22, 2023
@igoracmelo
Copy link
Author

@seankhliao Although they are similar, I wouldn't say it is a duplicate.
The other proposal is about "generic must", while this is a specific use case for environment variables.

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

No branches or pull requests

3 participants