79375388

Date: 2025-01-21 17:34:46
Score: 0.5
Natty:
Report link

The language specification says:

Each iteration has its own separate declared variable (or variables) [Go 1.22]. The variable used by the first iteration is declared by the init statement. The variable used by each subsequent iteration is declared implicitly before executing the post statement and initialized to the value of the previous iteration's variable at that moment.

In the question's first example, the anonymous function captures per-iteration variables.

In the question's second example, the anonymous function captures the single variable declared with var i int. This variable is incremented to 10 before the goroutines run.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Lauren Sanchez