top | item 44173149

(no title)

pikzel | 9 months ago

They still haven't solved shadowing.

  a, err := foo()
  b, err := bar()
  if err != nil { // oops, forgot to handle foo()'s err }
This is the illusion of safe error handling.

discuss

order

AnimalMuppet|9 months ago

I would be astonished if there isn't an automated tool to check for that at the push of a button. I would be mildly surprised if there isn't a compiler flag to check for it.

arp242|9 months ago

Not a compiler check, but staticcheck is widely used:

    % staticcheck test.go
    test.go:7:2: this value of err is never used (SA4006)

masklinn|9 months ago

There very much is not. There is a compiler error you can’t disable if a variable is completely unused and that is it.

catlifeonmars|9 months ago

I’m surprised I don’t see this mentioned more. This is spooky action at a distance at its worst. And it’s not even limited to error handling. Any multi value assignment works like this.

_benton|9 months ago

It's fairly obvious when writing Go that `err` is being shadowed and needs to be checked after each expression. You should be wrapping them anyways!