a common mistake when using Function Signatures
Fast recall
Quick revision
Core answer
Practical understanding of Function Signatures.
Repeating a definition without saying when to use it.
Give one trade-off and say how you would test it.
Interview-ready guide
Interview answer
The trap, demonstrated:
funcs := make([]func() int, 3)
for i := 0; i < 3; i++ {
funcs[i] = func() int { return i }
}
// pre-1.22: funcs[0](), funcs[1](), funcs[2]() → 3, 3, 3 (all the last value)
// 1.22+: → 0, 1, 2 (each iteration's own i) — executedThe pre-1.22 mechanics: the loop variable i was a single memory slot; every closure created in the loop captured THAT slot; when the closures ran (after the loop finished), the slot held 3. The goroutine variant was the classic production bug — a hundred goroutines all logging the last item.
The pre-1.22 fixes people wrote: pass it in (go func(v int) { ... }(i)) or shadow it (i := i inside the loop). Both work by giving each closure its own variable.
a common mistake when using Function Signatures
Direct Answer
Before Go 1.22, the for-loop variable was ONE variable reused across iterations. Closures capturing it all referenced the SAME slot — after the loop, they all saw the LAST value. 1.22 changed the semantics: each iteration gets a FRESH variable.
Deep Dive & Architecture
Go 1.22 changed the LANGUAGE: each iteration of a for-loop gets a fresh instance of the loop variable. The closures now capture different variables and return 0, 1, 2 (executed above). The change was safe because the old behavior was almost never intended — the bug dominated any legitimate use.
The interview framing: state the version. 'Since 1.22, each iteration gets a fresh variable — the capture bug is gone' is current truth; 'closures capturing the loop variable see the final value' is the historical truth that explains why millions of lines of Go have i := i in them and why linters still recognize the pattern. Both halves are knowledge; the version boundary is where they meet.
Common Mistakes & Pitfalls
Repeating a definition without saying when to use it.
Check your understanding
Answer these without looking back at the article.
- What is Function Signatures, and when would you use it?
- When would you use Function Signatures in a real project?
- How would you compare Function Signatures with an alternative?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.