3 ms·
Anyone know if the following exists? Gofmt/gofumpt, but as a special case, formats if err != nil { return err } on a single line. Not trolling, genuinel
by akdor1154 4y ago
Anyone know if the following exists?
Gofmt/gofumpt, but as a special case, formats
if err != nil { return err }
on a single line.
Not trolling, genuinely interested.
- FooBarWidget 4y agoI've found that it's almost always a good idea to return fmt.Errorf(...), adding context to the error. Otherwise error messages are too generic and it's hard to figure out what's actually wrong. Then it doesn't fit on a single line anymore.
- saurik 4y agoIt doesn't? It is only a tiny tiny bit longer than the fmt.Errorf() by itself, which is surely one line, right? The key thing is to then have your syntax highlighter set up to specially handle these lines, to help you 1) know that they are there, 2) know that they are structured correctly, and 3) ignore them (which is safe now that you can see it exists and is in the right format). Unless you set these up as one line, your Go code gets littered with 3x (or even 4x if you add a blank line after each block, which starts to feel better) as much error handling lines as non-error lines.
- therein 4y agoI enjoy errors.Wrap/Wrapf too.
- oefrha 4y agogithub.com/pkg/errors is dead — it’s been archived for a long time, and doesn’t incorporate go 1.20’s multi-error wrapping. Better switch to fmt.Errorf in new projects going forward.
- preseinger 4y agofmt.Errorf with %w is how you do this since Go 1.13, gotta drop that dep
- cesnja 4y agoThe default gofmt doesn't format this way, so any tool implementing this would not be compatible.
- DangitBobby 4y agoDoes gofmt have a problem with error handling on one line?
- lanstin 4y agoYou should really run gofmt on every file save.
- mseepgood 4y ago"return err" is almost never the right thing to do.