Reading — step 1 of 4
Learn
Errors as Values
Go has no exceptions. A function that can fail returns an error as its last result, and the caller decides what to do — right there, in plain control flow:
error is just an interface with one method:
Anything with an Error() string method is an error. That is the entire mechanism — no hierarchies, no throw, no invisible control flow. The payoff: every failure path is visible in the code, and the compiler complains about an unused err.
Creating errors
Wrapping: %w and the error chain
When you pass an error upward, add context. The %w verb (Go 1.13+) formats exactly like %v and records the original error so it can be inspected later:
The message is the prefix plus the original, e.g. parse: strconv.Atoi: parsing "abc": invalid syntax. The recorded chain matters because callers can query it without string matching:
errors.Is walks the chain comparing values; errors.As walks it looking for a target type. Both only see through links created with %w. Here is the trap: wrap with %v instead and the printed text is identical, but the chain is severed — errors.Is returns false and nobody notices until production. %v and %w print the same but behave differently.
Custom error types
When callers need structured data, define a type:
Comparing by string is the anti-idiom
if err.Error() == "not found" breaks the moment a message is reworded or a wrapper adds a prefix. Use sentinels with errors.Is, or types with errors.As. Raw message text belongs in logs — and in graders that diff your program's stdout, like this one.
Your exercise
Write validateAge(s string) (int, error):
strconv.Atoifails: return the error wrapped asfmt.Errorf("parse: %w", err)- parsed value is negative: return
errors.New("negative") - otherwise: return the value and
nil
main already prints age: <n> or error: <message>. The grader compares stdout exactly, so the wording is load-bearing:
- The prefix must be exactly
parse:. For inputabcthe expected line iserror: parse: strconv.Atoi: parsing "abc": invalid syntax. Return the raw error unwrapped and theparse:prefix is missing; invent your own message and nothing matches. - Negative means
< 0, not<= 0. The hidden test feeds0and expectsage: 0. Writeif n <= 0and you reject a perfectly valid age witherror: negative.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…