4 ms·
var foo *Foo if foo, err := getFoo(); err == nil { return nil, err } // foo is usable now
by lowmagnet 9y ago
var foo *Foo
if foo, err := getFoo(); err == nil {
return nil, err
}
// foo is usable now
- lobster_johnson 9y agoI suspect you don't know how := works. ./test.go:14:15: foo declared and not used
- flip_rect_red 9y agoit appeared to be a typo, and as you suggest the compiler caught it var foo *Foo var err error if foo, err = getFoo(); err != nil { return nil, err } //foo is usable now
- lobster_johnson 9y agoThat solves nothing. The point was that you pollute the scope with error variables, leading to dangerous logic errors that can be hard to spot. Your snippet is no better than: foo, err := getFoo() if err != nil { return nil, err } "err" still ends up in the scope.