Time: 0:00
Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).
This bug belongs to the async and race conditions pattern
A missing await, unbuffered channel deadlocks, ConcurrentModificationException, and data races under load.
What went wrong: The goroutine closure captures i and val by reference. Because the loop continues running while goroutines are scheduled, all goroutines observe the final iteration's values.
The fix: Pass i and val as explicit arguments into the anonymous goroutine function.
Remember: Always pass loop variables as arguments to goroutines to bind their value per iteration.
The pattern behind it: async and race conditions.
Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.
<iframe src="https://trybughunt.com/embed/4ca2e150-cb68-4035-a3fe-6fff84b4acaf" title="Debugging challenge: Loop variable captured by reference in goroutine" 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/4ca2e150-cb68-4035-a3fe-6fff84b4acaf">TryBugHunt</a></p>