0PricingLogin
Go Academy · Lesson

The error Interface

Returning and checking errors idiomatically

Go's Error Philosophy

Go handles errors as values, not exceptions. Functions return an error as the last return value. The caller must check it explicitly:

package main
import ("fmt"; "strconv")

func main() {
    n, err := strconv.Atoi("abc")
    if err != nil {
        fmt.Println("error:", err)  // handle it
        return
    }
    fmt.Println(n)
}

The error Interface

The built-in error is a simple interface:

// Built into Go — no import needed
type error interface {
    Error() string
}

// Any type with Error() string satisfies it
// nil means no error

All lessons in this course

  1. The error Interface
  2. Creating Custom Errors
  3. Error Wrapping and Unwrapping
  4. panic, recover and defer
← Back to Go Academy