3 ms·
Ignoring an error is a red herring. You have to go out of your way to actually use a special character to do it. No, one real issue that can happen if one is n
by aatd86 3y ago
Ignoring an error is a red herring.
You have to go out of your way to actually use a special character to do it.
No, one real issue that can happen if one is not careful (but fortunately linters help) is variable shadowing which may lead to some errors being unchecked.
In general, I find that error handling is not as horrible as some seem to purport.
- janosdebugs 3y ago> You have to go out of your way to actually use a special character to do it. Only if the function returns more than the error. You can happily do this without errors: fh = os.Create("/some/file") defer fh.Close() Needless to say, this is a terrible idea if the underlying filesystem can give you an error at close time, e.g. on NFS. The correct way to write the above code would be: fh = os.Create("/some/file") defer func() { if err := fh.Close(); err != nil { // Do something with the error } }() Yet, I see a lot of the former and very few instances of the latter.
- aatd86 3y agoOh you're right. I had forgotten about that. I think it's mostly an API legacy mistake. Close should probably return (bool, error). Probably a remnant of coding in C wrt sentinel values.
- janosdebugs 3y agoYou could still ignore both returned values. Go shouldn't allow ignoring returns without explicit dogsleds (underscore) at all if it were to stay "in character".