typestar

error is an interface in Go

One method, Error() string — anything with it is an error.

func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, errors.New("divide by zero")
    }
    return a / b, nil
}

func report() string {
    if _, err := divide(1, 0); err != nil {
        return "failed: " + err.Error()
    }
    return "ok"
}

How it works

  1. A function returns its error last, by convention.
  2. nil means success; check it before using the other results.
  3. errors.New is enough when there is nothing to carry.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
229
Tokens
70
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 44 seconds.

Type this snippet

Step 1 of 3 in The error value, step 1 of 15 in Errors.

Next →