Re:Portable Cloud Programming with Go Cloud
by ssambros on Tuesday July 24, 2018 @01:32PM
(#17603217)
Third line ':=' is not required, you can reuse globally scoped 'err' with '=' as well; unless you want to preserve the current value of global 'err' for future use (which would be confusing).
The third line has a new err inside the if statement's scope.
It's equivalent to
{ // begin new scope here
err := w.Close()
if err != nil {
foo
} else {
bar
}
}
Re:Portable Cloud Programming with Go Cloud
by elithrar on Tuesday July 24, 2018 @12:56PM
(#17602858)
> the third line is not really introducing a new variable, since err is still in scope... or is it?
The "if" block creates a new scope.
Re:Portable Cloud Programming with Go Cloud
by ironjunkie on Tuesday July 24, 2018 @02:20PM
(#17603668)
agreed that the := that can be used when one new var is introduced but not the others is extremely confusing.
I also hate the inconsistent behavior when you check multiple errors in a row, the first one will get := but not the second one (if you don't put those in a if statement, which I never do). But now if for some reason, you remove the first := statement, the code will error on the second one, that needs to be changed from = to :=
Not to mention, what is the last argument `nil` supposed to represent? In most high quality Node.js libraries, functions/constructors will take an optional options object so you can omit it, or pass an object like { timeout: 300 } to it.