HiveBrain v1.2.0
Get Started
← Back to all entries
debuggoMajor

Go Error Handling Techniques

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
errortechniqueshandling

Problem

I'm just getting started with Go. My code is starting to have a lot of this:

if err != nil {
      //handle err
   }


or this

if err := rows.Scan(&some_column); err != nil {
      //handle err
  }


Are there some good idioms/strategies/best-practices for checking and handling errors in Go?

EDIT to clarify: I'm not bellyaching or suggesting that the Go team come up with something better. I'm asking if I'm doing it right or have I missed some technique that the community came up with. Thanks all.

Solution

Your code is idiomatic and in my opinion it is the best practice available. Some would disagree for sure, but I would argue that this is the style seen all over the standard libraries in Golang. In other words, Go authors write error handling in this way.

Context

Stack Overflow Q#16963298, score: 64

Revisions (0)

No revisions yet.