top | item 41501069

(no title)

Woansdei | 1 year ago

Sometimes there is nothing you can do when there is an error, in that case there is no point in adding several layers of error forwarding until you ignore it somewhere higher up.

discuss

order

corytheboyd|1 year ago

Is… NOT ignoring errors just not an option? I don’t get it. If you propagate errors up but not all the way to being handled, haven’t you failed in a very simple, easy to fix way? Should you have a linter catching these things?

deergomoo|1 year ago

In this case the issue is that defer is a very good way to ensure you don’t forget to close the file in any branches, but a bad way to return values (you have to set the value of a named return variable, which is one of Go’s odder features).

> Should you have a linter catching these things?

JetBrains’ GoLand will in fact warn you of this. If the error truly is immaterial you can instead do

defer func() { _ = f.Close() }()

which is verbose but explicit in its intent to ignore the error.