Go traps · Chapter 7 of 16

Map iteration order is random

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

With only a few keys, the shuffle often lands the same way. This counts which of two keys comes first over 100,000 ranges:

kinds := map[string]int{"AC7": 1400, "DC30": 2000}
first := map[string]int{}
for range 100000 {
	for kind := range kinds {
		first[kind]++
		break
	}
}
fmt.Println(first)
map[AC7:87574 DC30:12426]

Seven times in eight, the same order. A test with two stations and one call passes most days, which is how this kind of bug ships.

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: a different file from the same totals

    Write reproduce() in repro.go, which returns an int. Make one map of totals, paise per station, for at least 8 stations (any ids and amounts), call ExportCSV(totals) from export.go 20 times with that same map, and return how many different files it gave.

    The check runs your reproduce() twice, each time in a fresh build of the case's folder: beside the original export.go it must return more than 1 (the same totals, different files), and beside a fixed export.go it must return 1. So it must really call ExportCSV 20 times with the same map. With 8 stations, 20 calls that all give the same file is about as likely as 1 in 10^17; with 2 stations it happens about 7 times in 100, so use 8 or more. No real waiting and no input: reproduce() must finish within 3 seconds.

  2. Task 2

    export.go: the same file for the bank every night

    Fix ExportCSV(totals map[string]int) string in export.go so it gives the same file on every call: the header line station,paise, then one line <station>,<paise> per station, sorted by station id (ST001 before ST002), each line ending in a newline. With no stations, or a nil map, it gives only the header line.

    Keep the header, the line format and the function's name and types as they are. The check calls ExportCSV many times with its own stations and amounts, some of them equal, and compares the whole file every time: sorting the lines by amount gives a steady-looking file, but not station order.

  3. Task 3

    export_test.go: a test that would have caught it

    Write a Go test file, export_test.go, that would have caught this bug before the bank saw it. The check runs your file three times, each in a fresh build: beside the original export.go, where at least one test must fail; beside the fixed export.go, where every test must pass; and beside a wrong fix that sorts the lines by amount, where at least one test must fail.

    So compare the exact file ExportCSV gives with the whole file you expect, in station order, and do it on every one of 50 calls, because a single call can come out in the right order by luck. Use at least 8 stations, most of them with the same amount, so a sort by amount cannot pass either. Each test must finish within a second or two (the whole file is stopped after 8 seconds).

What you will learn

  • repro.go: a different file from the same totals
  • export.go: the same file for the bank every night
  • export_test.go: a test that would have caught it

More in Everyday traps

Build "Map iteration order is random" 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.