TryBugHunt
goScope errorHard

Every reminder is addressed to the last person

BugFind the bug and fix the code

What it should do

reminderMessages(names) queues one reminder task per person and then runs them all. reminderMessages(["ana", "ben", "cy"]) returns ["Reminder for ana", "Reminder for ben", "Reminder for cy"].

What happens instead

reminderMessages(["ana", "ben", "cy"]) returns ["Reminder for cy", "Reminder for cy", "Reminder for cy"].

package main

func reminderMessages(names []string) []string {
    tasks := []func() string{}
    for _, name := range names {
        tasks = append(tasks, func() string {
            return "Reminder for " + name
        })
    }
    messages := []string{}
    for _, task := range tasks {
        messages = append(messages, task())
    }
    return messages
}

Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).

⏱️0:00
Hints
🏆 0 hints = Flawless Solve
Think & Test Cooldown
2:00 remaining

Take a moment to read the bug symptom and trace the code with Run & Check before unlocking Hint 1.

Hint 2
Unlocks after Hint 1
Hint 3
Unlocks after Hint 2

This bug belongs to the scope and closure bugs pattern

A variable redeclared with := so the outer one never changes, and loop variables captured late.

What you'll practise

A hard Go debugging exercise about scope and closure bugs: a variable redeclared with := so the outer one never changes, and loop variables captured late.

The checks your fix must pass

Signs of this kind of bug

Spoiler: why this bug happens(try it first; the hints get warmer one at a time)

A closure captures variables, not values. Before Go 1.22, a for range loop has a single name variable that is reassigned on every iteration, so all three closures refer to the same variable. They only run after the loop has finished, when name holds the last value. Declaring name := name inside the loop makes a fresh variable per iteration for each closure to capture. Go 1.22 changed loop variables to be per-iteration, but this runner uses an older Go, and plenty of production code still does.

The pattern behind it: scope and closure bugs.

More scope and closure bugs to practise

Embed this challenge on your site

Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.

<iframe src="https://trybughunt.com/embed/e242dc9f-7c5f-4fbd-952a-c82b861b5539" title="Debugging challenge: Every reminder is addressed to the last person" width="100%" height="580" loading="lazy" style="border:1px solid #d4d4d8;border-radius:12px;max-width:760px"></iframe>
<p style="font-size:14px">Debugging challenge from <a href="https://trybughunt.com/challenges/e242dc9f-7c5f-4fbd-952a-c82b861b5539">TryBugHunt</a></p>