4 ms·
> `if err != nil { return err }` is actually bad Go code, and not often written by good Go programmers. Like the people who wrote the Go stdlib? Because that's
by icebraining 7y ago
> `if err != nil { return err }` is actually bad Go code, and not often written by good Go programmers.
Like the people who wrote the Go stdlib? Because that's filled with those - just look at the net/* packages.
- steev 7y agoLegitimate question: then what does good Go code that is written by good Go programmers do/look like? Wrap `err` in `errors.New("some function failed", err)`?
- sagichmal 7y agoAt a minimum, an error should be logged with appropriate context and execution allowed to continue; or annotated and returned. Error annotation is currently best achieved with pkg/errors as e.g. `errors.Wrap(err, "error doing thing")`. (The xerrors suggestion of `fmt.Errorf("error doing thing: %w", err)` is awkward and hacky.) Many programs can benefit from a more structured approach to error management. But once you get past the minimum (above) there's no one-size solution for what "a more structured approach" looks like. I really enjoy how upspin.io does their errors package, though it is somewhat esoteric. I'm also reading and generally liking how Cockroach does things, though I don't like the coupling to Protobufs.
- mikeschinkel 7y agoRecently the Go team introduced the xerrors package for improved error management: https://godoc.org/golang.org/x/xerrors https://godoc.org/golang.org/x/xerrors So using these functions is becoming part of what good Go programmers do.
- qwsxyh 7y agoThat statement is still correct even in the context of the stdlib.
- sagichmal 7y agoOne distinction that is often made by core team members is that you only need to annotate errors at package boundaries, and that returning unannotated errors within a package is fine. But in most code, the packages are not so well-defined, or well-thought-out, or sacrosanct, that they represent a good proxy for annotation decisions. I would much rather have an error with too much or duplicate annotation than one with not enough. And I would further argue that, yes, in the stdlib, errors are generally under-annotated.