You're right, that's horrible. However, you
can also write it like this:
f := try(os.Open(file))
info := try(f.Stat())
This can be further improved by applying special syntax highlighting that makes the try less prominent.
Meanwhile, the cleanest way to write it right now seems to be:
f, err := os.Open(file)
if err == nil {
return err
}
info, err := f.Stat()
if err == nil {
return err
}
That's 8 lines of code to do the same thing. Did you spot the bug I introduced, or did you miss it because the volume of code made you skim? (error check has == instead of !=)?
If you want to judge code by the worst thing you can do, you'd need to compare with things like:
var info os.FileInfo
if f, err := os.Open(file); err != nil {
info, err = f.Stat()
}
if err != nil {
return err
}
(Did you spot the bug? If err is already declared somewhere further up, this will compile <
https://play.golang.org/p/0l-SsEbKQh2>, but the "err" within the if shadows the previously declared one, so the errors from os.Open and f.Stat are never checked.)