top | item 18534436

(no title)

NateDad | 7 years ago

If you care, you can't realistically forget. There are linters that will find your missed error checks.

discuss

order

whateveracct|7 years ago

This is classic Go design, for better or worse. "Just use this ad-hoc, unprincipled tool to counter a shortcoming of the core language."

hu3|7 years ago

In Go, if a function returns an error you have to explicitly handle or suppress it. Otherwise code just doesn't compile.

There's no margin to "forget".

jerf|7 years ago

It's true that functions that return (result, error) tuples are hard to miss the errors from, because you have to explicitly either at least accept it, or ignore it. It's both visually obvious that you've dropped the error and there are linters to catch this for you (which I highly recommend integrating into pre-commit hooks).

However, if a function just returns an error, but no result value, it is indeed easy to silently drop that error without realizing it by simply invoking the function and not catching the result at all. To which, again, I'd highly recommend using linters at commit time or even code save time.

jcelerier|7 years ago

> In Go, if a function returns an error you have to explicitly handle or suppress it. Otherwise code just doesn't compile.

ah, yeah, great idea. in practice this just means that most Go projects are minefields of

    if err != nil { panic(err) }
or

    if err != nil { return err }
ie a manual & repetitive implementation of assert for the first case and manual & repetitive implementation of an exception call stack for the second. thank you very much, I'll take the automated version known as exceptions instead.

weberc2|7 years ago

This isn’t true. This is a valid program, after all—even though fmt.Println returns (int, error):

    func main() {
        fmt.Println(“Hello”)
    }

masklinn|7 years ago

> In Go, if a function returns an error you have to explicitly handle or suppress it.

If a function only returns an error, or if it returns both a value and an error and you care for neither, you can absolutely ignore them entirely.

And when you can "suppress" an error and still access the value, it's not really impressive.

silasdavis|7 years ago

file, _ := ioutil.ReadFile(filename)