4 ms·
You didn't copy the code correctly from the first example.
by terminalbraid 1y ago
You didn't copy the code correctly from the first example.
- gwd 1y agoWell no, the second "if" statement is a red herring. Both of the following work: bar, err := foo() if err != nil { return err } if err = foo2(); err != nil { return err } and bar, err := foo() if err != nil { return err } if err := foo2(); err != nil { return err } He even says as much: > Even if we change that to :=, we’re left to wonder why err is in scope for (potentially) the rest of the function. Why? Is it read later? My initial reaction was: "The first `err` is function-scope because the programmer made it function-scope; he clearly knows you can make them local to the if, so what's he on about?` It was only when I tried to rewrite the code to make the first `err` if-scope that I realized the problem I guess he has: OK, how do you make both `err` variable if-scope while making `bar` function-scope? You'd have to do something like this: var bar MyType if lbar, err := foo(); err != nil { return err } else { bar = lbar } Which is a lot of cruft to add just to restrict the scope of `err`.