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
- A function returns its error last, by convention.
nilmeans success; check it before using the other results.errors.Newis enough when there is nothing to carry.
Keywords and builtins used here
errorfloat64funcifreturnstring
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.
Step 1 of 3 in The error value, step 1 of 15 in Errors.