Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumGotable-driven testsSingle-choice MCQ

In a table-driven test using t.Run with subtests, why is the common pattern to capture the loop variable (e.g. tt := tt) historically recommended, and what changed?

for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() got := Add(tt.a, tt.b) if got != tt.want { t.Errorf("got %d want %d", got, tt.want) } }) }