Go traps · Chapter 5 of 16

append shares the backing array

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

Both appends are ordinary, legal Go, and go vet says nothing. A test that makes one plan, or checks plans one at a time right after making them, passes: the overwrite happens only when a second append reuses the room.

The usual fixes give each result its own array: cap the slice at its length with a full slice expression, base[:len(base):len(base)], so append must copy, or append to slices.Clone(base).

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: swap two drivers' last stops on purpose

    Write reproduce() in repro.go, which returns a []string. Build a route's common stops with Route(rows) from plan.go, from three stops like the ticket's (ST001, ST044 and ST131). Make a plan for a first driver with Plan(common, last) and one last stop, then a plan for a second driver from the same common stops with another last stop, and return the first driver's plan.

    The check runs your reproduce() twice, each time in a fresh build of the case's folder, and compares the two plans: they must hold the same common stops and end differently, in the second driver's last stop beside the original plan.go (like []string{"ST001", "ST044", "ST131", "ST118"}) and in the first driver's own beside a fixed plan.go. So make the second plan before you return the first. No real waiting and no input: reproduce() must finish within 3 seconds.

  2. Task 2

    plan.go: every driver keeps their own plan

    Fix Plan(common []string, last string) []string in plan.go so every plan it returns is the common stops then last, in a slice that shares no array with common or with any other plan. After any number of plans from the same common stops, each plan still ends in its own last stop, and common is unchanged, with nothing written into the spare room of its array.

    Keep Route, PlanTrips and its line plan <driver>: <the stops joined by " > "> as they are. The check makes many plans from routes of one to seven stops, with and without spare room, and compares whole plans: a plan must still hold every common stop.

  3. Task 3

    plan_test.go: a test that would have caught it

    Write a Go test file, plan_test.go, that would have caught this bug before shared routes went live. The check runs your file three times, each in a fresh build: beside the original plan.go, where at least one test must fail; beside the fixed plan.go, where every test must pass; and beside a wrong fix whose plans hold only the last stop, where at least one test must fail.

    So make two plans from one route built with Route from three stops, then compare both whole plans with what you want: every common stop, then each driver's own last stop. Each test must finish within a second or two (the whole file is stopped after 8 seconds).

What you will learn

  • repro.go: swap two drivers' last stops on purpose
  • plan.go: every driver keeps their own plan
  • plan_test.go: a test that would have caught it

More in Everyday traps

Build "append shares the backing array" 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.