Go traps · Chapter 13 of 16

A map written by many goroutines

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Defer
  • 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.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.go: heartbeats from twenty chargers at once

    Write reproduce() in repro.go, which returns an int. Make a LastSeen with NewLastSeen() from lastseen.go, start 20 goroutines at once, each calling Record for its own charger, C1001 to C1020, with any time, wait for all of them, and return how many of those 20 chargers Get finds.

    The check runs your reproduce() twice, each time in a fresh build of the case's folder with the race detector on: beside the original lastseen.go the race detector must report a data race (or the runtime must crash with concurrent map writes), and beside a fixed lastseen.go there must be no race and it must return 20. So the goroutines must really record at the same time: start them all before you wait for any. No real waiting and no input: reproduce() must finish within 3 seconds.

  2. Task 2

    lastseen.go: heartbeats from every charger at once

    Fix LastSeen in lastseen.go so any number of goroutines can use it at once. Record(charger string, at time.Time) notes the heartbeat, and an older one never replaces a newer one; Get(charger string) (time.Time, bool) returns the latest time and true, or the zero time and false for a charger that never sent one; NewLastSeen() *LastSeen returns an empty LastSeen ready to use. Keep these names and signatures and the out-of-order rule.

    The check runs under the race detector with its own chargers and times: it calls Record and Get from many goroutines at the same time, then checks every charger's latest time. A lock around the writes only still lets a Get read the map while a Record changes it, and the race detector reports that too.

  3. Task 3

    lastseen_test.go: a test that would have caught it

    Write a Go test file, lastseen_test.go, that would have caught this bug before Monday's release. The check runs your file three times, each in a fresh build with the race detector on: beside the original lastseen.go, where at least one test must fail; beside the fixed lastseen.go, where every test must pass; and beside a wrong fix that locks Record only, where at least one test must fail.

    So in one test, start goroutines that call Record and others that call Get, all before you wait for any, then check the times Get gives. Under the race detector a test fails by itself when two goroutines use the map without the lock. Each test must finish within a second or two (the whole file is stopped after 8 seconds).

What you will learn

  • repro.go: heartbeats from twenty chargers at once
  • lastseen.go: heartbeats from every charger at once
  • lastseen_test.go: a test that would have caught it

More in Defer

Build "A map written by many goroutines" 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.