Let’s consider this situation:
func main() {
file1, err := os.Open("file1");
if err != nil {
log.Fatalln(err)
}
defer file1.Close()
// What happens if this fail?
file2, err := os.Open("file2");
if err != nil {
log.Fatalln(err)
}
defer file2.Close();
}
The problem with the previous code is that, if the application fails to open “file2”, the first file won’t be closed, since log.Fataln exits immediately from the program and does not execute the “defer” functions. But the previous code can be fixed easily by using log.Panic.
What do you think? Is it good to use log.Panic instead of log.Fatal?