Go traps · Chapter 1 of 16
A nil error that is not nil
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Everyday traps
- 3 tasks
- Go
- Free chapter
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.
Why you need this
A function whose result is error turns whatever it returns into an error. Returning the nil pointer compiles, go vet says nothing, and the caller's if err != nil is true every time:
func Ready(charger, version string) error {
return checkFirmware(charger, version)
}
not ready: <nil>
fmt prints <nil> because the pointer inside is nil, which hides the type that makes the error real. The usual fix: look at the pointer while it is still a pointer, and return a plain nil when all is well.
The lesson
By the end of this case
You can tell a nil pointer hiding inside an error from its symptoms, make it happen on purpose, fix it, and pin it down with a Go test that fails on the bug and passes on the fix.
The ticket
BG-712: Drivers with money refused at every charger. Assigned to you by Joseph. Priority: high. Since: Friday's release.
Nandini, in the ops channel at 9:20 am: "Drivers are stuck at our chargers since Friday night. D10422 had ₹500 in the wallet at Malleshwaram Metro, C1042 needs ₹200 up front, and the app said no. Three fleets have called. We are losing the whole morning."
The authorize service's log for D10422:
09:12:44 authorize authorize D10422 at C1042: refused: <nil>
Joseph: "Friday's release changed one thing in wallet.go: checkBalance now returns a *WalletError instead of an error, so the app can show how much to top up. Look at the log: refused, and the reason is <nil>. Reproduce it first, then fix it, then make sure it can never come back. You are in wallet.go."
An interface is a pair
error is an interface. An interface value holds two things: the type of what was put in it, and the value. It is nil only when both are empty. Here is a firmware check of the same shape:
func checkFirmware(charger, version string) *FirmwareError {
if version < "2.4.1" {
return &FirmwareError{charger, version}
}
return nil
}
func main() {
p := checkFirmware("C1203", "2.5.0")
var err error = p
fmt.Println(p == nil, err == nil)
fmt.Printf("%v %T\n", err, err)
var none error
fmt.Printf("%v %T\n", none, none)
}
true false
<nil> *main.FirmwareError
<nil> <nil>
The pointer is nil. The error holding it is not: its type is *main.FirmwareError. Only an error that was never given anything has type <nil>.
Making it happen on purpose
A bug you can only see in production is a bug you cannot fix with confidence. So you reproduce it first: the same wrong result, on demand, every run. In this pack every case has repro.go with a function reproduce() that drives the production code into the bug and returns what the code gave back. It never checks anything itself.
repro.go is in the same package as the code, so it calls the functions directly. Its main prints what reproduce() returned, with %#v, the way the check sees it:
$ go run .
authorize D10422 at C1042: refused: <nil>
"refused"
The check runs your reproduce() beside the original code, where it must give the bug, and beside a fixed version, where it must give the healthy answer. A repro that returns the word without calling the code fails the second run.
A test that would have caught it
A guard test turns the repro into something that runs before every release. A Go test lives in a file ending in _test.go, in a function func TestSomething(t *testing.T), and t.Fatalf fails it with a message:
func TestReadyOnNewFirmware(t *testing.T) {
if err := Ready("C1203", "2.5.0"); err != nil {
t.Fatalf("Ready on 2.5.0 got %v, want nil", err)
}
}
Beside the buggy Ready it fails, and beside the fixed one it passes:
$ go test
--- FAIL: TestReadyOnNewFirmware (0.00s)
fw_test.go:7: Ready on 2.5.0 got <nil>, want nil
FAIL
$ go test
PASS
ok bijligo/chapters/firmware-demo 0.002s
A good guard fails on the bug and passes on the fix. Test both sides: a Ready that always returns nil passes this test too.
Your turn
Three tasks, one file each. In repro.go, make a driver with money get refused on purpose, and run it with go run .. In wallet.go, fix Authorize. In wallet_test.go, write the guard: the check runs it beside the original code, the fix, and a wrong fix that mends StartSession only, so test Authorize itself. Then answer Joseph's interview question at the bottom of the case, in your own words.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.go: refuse a driver with money on purpose
Write reproduce() in repro.go, which returns a string. Call StartSession(driver, charger, balance, need) from wallet.go for a driver whose balance is more than need (any driver and charger, amounts in paise), and return what StartSession returned.
The check runs your reproduce() twice, each time in a fresh build of the case's folder: beside the original wallet.go it must return "refused" (a driver with money turned away), and beside a fixed wallet.go it must return "started". So it must really call StartSession with enough money. No real waiting and no input: reproduce() must finish within 3 seconds.
- Task 2
wallet.go: drivers with money can charge again
Fix Authorize(driver string, balance, need int) error in wallet.go. When balance is at least need, it must return a nil error, so StartSession prints authorize <driver> at <charger>: started and returns "started". When balance is less than need, it must return an error that errors.As turns into a *WalletError with Driver, Balance and Need set, whose message stays driver <id> has <balance> paise, needs <need>; StartSession then prints authorize <driver> at <charger>: refused: <that message> and returns "refused".
Keep checkBalance, WalletError and StartSession's two log lines as they are. The check calls Authorize itself with its own drivers and amounts (an exact balance counts as enough), not only StartSession: a fix inside StartSession alone leaves every other caller broken.
- Task 3
wallet_test.go: a test that would have caught it
Write a Go test file, wallet_test.go, that would have caught this bug before the release. The check runs your file three times, each in a fresh build: beside the original wallet.go, where at least one test must fail; beside the fixed wallet.go, where every test must pass; and beside a wrong fix that mends StartSession only, where at least one test must fail.
So test Authorize itself, on both sides: a driver with enough money gets a nil error, and a driver short of money gets an error. Each test must finish within a second or two (the whole file is stopped after 8 seconds).
What you will learn
- repro.go: refuse a driver with money on purpose
- wallet.go: drivers with money can charge again
- wallet_test.go: a test that would have caught it
More in Everyday traps
Build "A nil error that is not nil" yourself
BijliGo runs its EV chargers on Go, and Joseph, its senior engineer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.