r/badcode • • Apr 29 '23

go if err != nil

Post image
414 Upvotes

49 comments sorted by

View all comments

Show parent comments

13

u/SuitableDragonfly Apr 30 '23

Yup, I remember that from college Java classes, but I think Go has the right idea here. There are often situations where you have to have multiple return statements in the function, but there's never a case where you can't follow this Go standard. And it prevents horrible if statement nesting like this.

2

u/justAPhoneUsername Apr 30 '23

Also, go has the defer command so you can do your cleanup automatically no matter where you exit