Re:Why I love Go
by lobster_johnson on Tuesday December 05, 2017 @11:44PM
(#15858589)
I suspect you don't know how := works.
[ Parent
| on HN ]
Re:Why I love Go
by flip_rect_red on Wednesday December 06, 2017 @02:56AM
(#15859356)
it 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
Re:Why I love Go
by lobster_johnson on Wednesday December 06, 2017 @03:22AM
(#15859500)
That 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.