How do I do go error handling idiomatic way


I have a goroutine with logic:

result1, err := getResult1()
result2, err := getResult2(result)
result3, err := getResult3(result2)

If I have an error in getResult1(), I would not like to continue this goroutine at all, but no panic, because it destroy the main too. I’ve implemented it in an awful way, but what would be the idiomatic solution?


The idiomátic way is do something

r, err := get()
if err != nil {
    // do something with err
    // do not continue

You can return the error to the caller, and the caller decide what to do.

You don’t need to panic. You can just log.

Or use the error in some loop to retry

Answered By – Tiago Peczenyj

Answer Checked By – Gilberto Lyons (GoLangFix Admin)

Leave a Reply

Your email address will not be published.