OK, I have the following code:
// Gets an environment variable and parse it.
expiration, err := time.ParseDuration(os.Getenv("expiration"))
if err != nil {
panic(err)
}
// Gets an environment filename and verifies that it exists.
dbSource := os.Getenv("filename")
if _, err := os.Stat(dbSource); err != nil && os.IsNotExist(err) {
panic(err)
}
But the second piece of code throws the following warning message:
declaration of “err” shadows declaration at ./main.go:33
It can be fixed quickly by replacing the second code by:
// Gets and environment filename and verifies that it exists.
dbSource := os.Getenv("filename")
// I changed ":=" by "="
if _, err = os.Stat(dbSource); err != nil && os.IsNotExist(err) {
panic(err)
}
But doing it this way creates a dependency between the first and the second piece of code. I mean, if I remove the first piece of code the second code won’t work. And they are different things.
How would you solve this situation in a elegant way?