(no title)
elvlysh | 11 months ago
type errHandler struct {
err error
}
func (eh *errHandler) getFirst() string {
// stuff
if err { eh.err = err }
return result
}
func (eh *errHandler) doWith(input string) string {
if eh.err != nil {
return ""
}
//stuff
if err { eh.err = err }
return result
}
func (eh *errHandler) doFinally(input string) string {
if eh.err != nil {
return ""
}
//stuff
if err { eh.err = err }
return result
}
func (eh *errHandler) Err() error {
return eh.err
}
func main() {
eh := &errHandler{}
first := eh.getFirst()
second := eh.doWith(first)
final := eh.doFinally(second)
if err := eh.Err(); err != nil {
panic(err)
}
}
9rx|11 months ago
But look at what you could have wrote:
This one is actually quite nice to read, unlike the others, and provides a better experience for the caller too – which is arguably more important than all other attributes.Dylan16807|11 months ago
I think something similar to the second one is also nice to read, and it gives the same improved experience to the caller as your suggestion.
unknown|11 months ago
[deleted]
elvlysh|11 months ago
I didn't know about this trick, thanks for sharing.
tubthumper8|11 months ago
Do people actually do this? Is it included in the standard library? If not, should it be?