Hacker News new | past | comments | ask | show | jobs | submit login

> The other bit of good news is that you can't unknowingly ignore a returned error, like you can with an unchecked exception. The compiler will force you at a minimum to declare the error as _, and tools like errcheck do a good job of keeping you honest.

Actually, we unknowingly ignore returned errors much more often than we think, like when we call a function and opt out of assigning any of the return values to variables. Consider this function, which returns a single value (being an error).

    func Failure() error {...}
You can always choose to call an error-returning function without declaring any placeholder (`_`):

    Failure()
There are several commonly used functions that return errors that are regularly ignored. How about `io.Writer`?

    writer.Write([]byte("Hello")) // returns (n int, err error)
It's quite common to call that function without feeling a need to check on the bytes written or a possible error. Or, consider whether you consistently check the return values of `fmt.Println()`, which also returns `(n int, err error)`...



errcheck is a good tool to help with this.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: