Go traps · Chapter 10 of 16

defer in a loop

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.

Why you need this

It is correct Go: the compiler, go vet and go test say nothing, and every file is closed in the end. The limit is the operating system's: a process may hold so many open files, often 1,024. A quiet night, or a test with a handful of files, stays under it. In the lab the limit is far higher:

$ ulimit -n
1048576

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: every file open at once, on purpose

    Write reproduce() in repro.go, which returns an int. Build a fake opener that hands out meter files from memory: a struct with a method Open(name string) (io.ReadCloser, error) that adds one to a count of open files and keeps the most open at one moment, and returns a file whose Close takes one away (a struct holding a strings.Reader and the opener). Call ImportAll(opener.Open, names) from importer.go for 50 good files (each line a whole number of watt-hours), and return the most files that were open at once.

    The check runs your reproduce() twice, each time in a fresh build of the case's folder: beside the original importer.go it must return 50 (every file still open at the same time), and beside a fixed importer.go it must return 1. So it must really import 50 good files through the counting opener. No real files, no real waiting and no input: reproduce() must finish within 3 seconds.

  2. Task 2

    importer.go: one meter file open at a time

    Fix ImportAll(open Opener, names []string) (int, error) in importer.go so each file is closed as soon as it has been read, on every way out, and never more than one is open at a time. With all files good it returns the number of readings in all of them and a nil error. At a line that is not a reading it returns the readings so far and the error <file> line <n>: bad reading "<line>", and does not open the files after it. When a file cannot be opened it returns the error file <i> of <n>: <the opener's error>, wrapped so errors.Is finds the opener's error.

    Every file ImportAll opened must be closed before it returns. Keep Opener, the error messages and ImportAll's name and types as they are. The check uses its own fake opener, which counts the files open at once and every Close, with a bad line in the middle of a file: a fix that closes a file only when it was read to the end leaves that one open.

  3. Task 3

    importer_test.go: a test that would have caught it

    Write a Go test file, importer_test.go, that would have caught this bug before the nightly import. The check runs your file three times, each in a fresh build: beside the original importer.go, where at least one test must fail; beside the fixed importer.go, where every test must pass; and beside a wrong fix that closes a file only when it was read to the end, where at least one test must fail.

    So import several good files through a fake opener and check never more than one was open at a time, and import files with a bad line in the middle of one and check every file opened was closed. The test file is in the same package as repro.go, so it can use the fake opener you wrote there (do not declare it twice). Each test must finish within a second or two (the whole file is stopped after 8 seconds).

What you will learn

  • repro.go: every file open at once, on purpose
  • importer.go: one meter file open at a time
  • importer_test.go: a test that would have caught it

More in Defer

Build "defer in a loop" 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.