Go · Chapter 5 of 26
Errors
By the end of this chapter you can return an error from a function and check one with if err != nil, read the errors the standard library gives you, add context to an error without losing it, test for a particular error with errors.Is, and say why Go returns errors where other languages throw exceptions, and when panic is the right call. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The language
- 4 tasks
- Go
- In the all labs plan
The first 2 chapters of Go are free. This one comes with the all labs plan.
Why you need this
Every meter line a charger sends is text: C1042,18437, the charger's id and the energy in watt-hours. Most lines are fine. On Thursday, one charger with old firmware sent 18,437, with a comma in the number. The code that read it crashed, and because it crashed, the whole ingest program went down with it: 1,180 chargers lost their service at once over one bad line from one of them. Tenzin was paged again.
Nothing you know so far can say "this went wrong" except crashing. A function from chapter 4 returns a number, and there is no number that means "that was not a number". What BijliGo needs is for every function that can fail to say so, for the caller to decide what to do (skip that line, log it, ask the charger again), and for everything else to carry on. In Go, a failure is an ordinary value, returned like any other: an error.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
reading.go: read a meter line, or say why not
A charger sends its meter as one line of text: its id, a comma, and the energy in watt-hours. Write ParseReading(line string) (string, int, error) in reading.go: it returns the charger's id, the energy, and an error.
ParseReading("C1042,18437") returns "C1042", 18437, nil, and ParseReading("C2180,0") returns "C2180", 0, nil. These must return an error: "C1042 18437" (no comma), ",18437" (no charger id), "C1042,18,437" (18,437 is not a number), "C1042,-5" (negative energy) and "" (empty). Whenever the error is not nil, the other two results are "" and 0.
For a number that does not parse, wrap strconv.Atoi's own error with %w in fmt.Errorf, so errors.Is(err, strconv.ErrSyntax) is true for "C1042,18,437".
- Task 2
tariff.go: a tariff, or a sentinel error
In tariff.go, give the variable ErrUnknownTariff its value: an error made with errors.New whose message is exactly "unknown tariff". Then write TariffFor(kind string) (int, error): it returns AC7 1400, DC30 2000, DC60 2400 and LEV3 900 (paise a kWh), each with a nil error.
Any other kind (the Check tries "AC22", "dc30" and "") returns 0 and an error that wraps ErrUnknownTariff with %w, so errors.Is(err, ErrUnknownTariff) is true, and whose message names the kind: the error for "AC22" must contain AC22.
- Task 3
station.go: which station a charger is at
Write StationOf(id string) (string, error) in station.go: it takes a charger id and returns the name of the charger's station, looking the charger up with bijli.ChargerByID and then its station with bijli.StationByID (the charger's Station field is the station's id). StationOf("C1042") returns "Malleshwaram Metro", nil; StationOf("C1001") returns "Koramangala Mall", nil; StationOf("C1051") returns "Sarjapur Road Metro", nil.
For a charger BijliGo does not have (the Check tries "C9999", "" and "c1042"), it returns "" and an error that wraps bijli.ErrUnknownCharger with %w, so errors.Is(err, bijli.ErrUnknownCharger) is true. The error's whole message for C9999 is exactly charger C9999: unknown charger.
- Task 4 · interview question
Interview: settle.go, the bill that crashed everything
Settle(whText string, tariff int) (int, error) in settle.go bills a meter's energy, sent as text, at a tariff, rounded to the nearest paisa. When a charger sent its energy as "18,437", Settle panicked and took the whole ingest program down with it. Fix it so that it never panics: Settle("17385", 2000) returns 34770, nil; Settle("18437", 2400) returns 44249, nil; Settle("0", 900) returns 0, nil. For "18,437", "-5", "" and "18437 Wh" (each at 2000) it returns 0 and an error. The error for "18,437" wraps strconv.Atoi's own error with %w, so errors.Is(err, strconv.ErrSyntax) is true.
Joseph will ask: Go has no exceptions. Why does it return errors as values, and when is panic the right thing?
What you will learn
- error and errors.New: a value that describes what went wrong.
- (value, error) results and if err != nil: the shape of every Go call that can fail.
- strconv.Atoi's error, and strings.Cut to split a line.
- Wrapping with fmt.Errorf and %w: context added, the original kept.
- Sentinel errors and errors.Is (also with bijli.ErrUnknownCharger), and errors.As in one paragraph.
- panic: for what must never happen, and why Go has no exceptions.
More in The language
Build "Errors" yourself
BijliGo, a Bengaluru EV charging network, drops chargers at the evening peak. You rewrite its backend in Go. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Go are free. This one comes with the all labs plan.