Go traps · Chapter 8 of 16

JSON's omitempty and unexported fields

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

It compiles, it runs, and go test passes. Only the full go vet notices the tag on a field nobody can see (the test's output trimmed):

$ go vet .
main.go:11:2: struct field faulted has json tag but is not exported
$ go test
PASS

go test runs only a few of vet's checks before the tests, and the struct tag check is not one of them.

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: an empty wallet's JSON on purpose

    Write reproduce() in repro.go, which returns a string. Call WalletJSON(driver, paise) from api.go for a driver with 0 paise in the wallet (any driver id), and return the JSON it gave.

    The check runs your reproduce() twice, each time in a fresh build of the case's folder: beside the original api.go the JSON must have the driver but no balance and no low_balance (for D10422 it is "{\"driver\":\"D10422\"}"), and beside a fixed api.go it must have "balance":0 and "low_balance":true. So it must really call WalletJSON for an empty wallet. No real waiting and no input: reproduce() must finish within 3 seconds.

  2. Task 2

    api.go: every wallet sends its balance and its banner

    Fix WalletJSON(driver string, paise int) string in api.go so every body has three fields, in this order: driver, balance (the paise, also when it is 0) and low_balance (true when the balance is below 20000 paise, ₹200, and false otherwise, so exactly 20000 is false). For D10422 with 0 paise it is {"driver":"D10422","balance":0,"low_balance":true}.

    Keep WalletJSON's name and types, the JSON names driver, balance and low_balance, and the ₹200 limit as they are. The check compares the whole body for its own drivers and amounts (0, low ones, exactly 20000 and a high one): a field left out when it is 0 or false fails, whichever tag option leaves it out.

  3. Task 3

    api_test.go: a test that would have caught it

    Write a Go test file, api_test.go, that would have caught this bug before Tuesday's release. The check runs your file three times, each in a fresh build: beside the original api.go, where at least one test must fail; beside the fixed api.go, where every test must pass; and beside a wrong fix that sends low_balance but still leaves out a balance of 0 (the omitzero tag), where at least one test must fail.

    So call WalletJSON for an empty wallet and check the body has "balance":0 and "low_balance":true, and call it for a high balance too, where low_balance is false. Comparing the whole JSON string is the simplest way. Each test must finish within a second or two (the whole file is stopped after 8 seconds).

What you will learn

  • repro.go: an empty wallet's JSON on purpose
  • api.go: every wallet sends its balance and its banner
  • api_test.go: a test that would have caught it

More in Everyday traps

Build "JSON's omitempty and unexported fields" 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.