Go traps · Chapter 6 of 16
The range variable is a copy
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
- In the all labs plan
The first 2 chapters of Go traps are free. This one comes with the all labs plan.
Why you need this
The loop reads d in the comparison and writes a field of it, so the compiler sees nothing unused, and go vet says nothing. The count in the ticket's log is right because the copy has the right station: only the change is lost.
You may remember a loop-variable trap with closures and goroutines. Since Go 1.22 every iteration has its own variable, which fixed that one; the variable is still a copy of the element, so this trap is unchanged.
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: a power cut that changes nothing, on purpose
Write reproduce() in repro.go, which returns a []string. Make a slice of three chargers at one station, all "available" (Charger is in status.go, with ID, Station and Status), call MarkOffline(chargers, station) with it and that station, then return the three chargers' statuses, read from your slice after the call.
The check runs your reproduce() twice, each time in a fresh build of the case's folder: beside the original status.go no status may be "offline" (like []string{"available", "available", "available"}), and beside a fixed status.go every status must be "offline". MarkOffline returns 3 either way, so read the statuses from the slice you passed in. No real waiting and no input: reproduce() must finish within 3 seconds.
- Task 2
status.go: a power cut really marks chargers offline
Fix MarkOffline(chargers []Charger, station string) int in status.go so it changes the caller's slice: every charger at the station gets Status "offline", every charger elsewhere stays exactly as it was, every charger stays in its place, and it returns how many it marked (0 for a station with no chargers, or for no chargers at all).
Keep Charger, PowerCut and its line power cut at <station>: <n> chargers marked offline as they are. The check looks at the slice it passed in, with its own chargers and stations. Taking the address of the loop variable still changes a copy.
- Task 3
status_test.go: a test that would have caught it
Write a Go test file, status_test.go, that would have caught this bug before the grid alert went live. The check runs your file three times, each in a fresh build: beside the original status.go, where at least one test must fail; beside the fixed status.go, where every test must pass; and beside a wrong fix that changes a pointer to the loop variable, where at least one test must fail.
So build a slice of chargers, call MarkOffline, then read the Status of the chargers in the slice you passed in, not only the count: the station's chargers must be "offline", and a charger at another station must keep its status. Each test must finish within a second or two (the whole file is stopped after 8 seconds).
What you will learn
- repro.go: a power cut that changes nothing, on purpose
- status.go: a power cut really marks chargers offline
- status_test.go: a test that would have caught it
More in Everyday traps
Build The range variable is a copy 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.
The first 2 chapters of Go traps are free. This one comes with the all labs plan.