Essential Go Error handling  Edit on GitHub      File Issue

Returning an error

In Go you don’t raise an error. Instead, you return an error in case of failure.

// This method can fail
func DoSomething() error {
    // functionThatReportsOK is a side-effecting function that reports its
    // state as a boolean. NOTE: this is not a good practice, so this example
    // turns the boolean value into an error. Normally, you'd rewrite this
    // function if it is under your control.
    if ok := functionThatReportsOK(); !ok {
        return errors.New("functionThatReportsSuccess returned a non-ok state")

    // The method succeeded. You still have to return an error
    // to properly obey to the method signature.
    // But in this case you return a nil error.
    return nil

If the method returns multiple values (and the execution can fail), then the standard convention is to return the error as the last argument.

// This method can fail and, when it succeeds,
// it returns a string.
func DoAndReturnSomething() (string, error) {
    if os.Getenv("ERROR") == "1" {
        return "", errors.New("The method failed")

    s := "Success!"

    // The method succeeded.
    return s, nil

result, err := DoAndReturnSomething()
if err != nil {
  ↑ ↓ to navigate     ↵ to select     Esc to close