On Saturday, 8 February 2020 07:02:34 UTC, addi t0t08 wrote: > > I think the keyword we are looking for is `pass`. Similarly, `pass` only > needs to return if err != nil. > > func writeSomething() error { > f, err := os.Open("file.dat") > pass err > defer f.Close() > > f.WriteString("...") > } > --- > passing a wrapped error(using the Wrap function above which returns nil if > err is nil (not a special function) ): > > > func writeSomething() error { > f, err := os.Open("file.dat") > pass errors.Wrap(err, "couldn't open file") > defer f.Close() > > f.WriteString("...") > } > > In the second example, how does it know that "err" is the value to check against nil? If I wrote
pass foo.Foo(bar, baz, qux) which of bar, baz or qux is checked against nil? Is it looking for an actual variable called "err" ? Secondly: it doesn't capture the common case of returning a new (and not wrapped) error. if err != nil { return DatabaseConnectionError } Wrapping errors isn't always a good idea: it exposes your implementation details. The API of the services you consume becomes part of the public API that you expose to your clients, which clients will then depend on. You can therefore not change your implementation to consume a different set of services. Also, I don't see how your proposal handles functions which return more values than just an error: if err != nil { return nil, nil, DatabaseConnectionError } "pass should work with 100% of the cases within its limited scope." - in other words, not all cases. It belongs to Eric's category 1: "Identify a solution that captures the 80-90% case of if err != nil { return ... }". I prefer the explicit if err != nil to this. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/12ffcdfb-25eb-42f8-aa39-d6575358184c%40googlegroups.com.