Sobes.tech
Junior — Senior

Printing indices from goroutine in a loop with variable capture

livecode

Task condition

It is necessary to investigate how goroutines work when launched inside a loop and to predict which numbers will be printed, considering that the anonymous function captures the loop variable.

package main

import (
  "fmt"
)

func main() {
  for idx := 0; idx < 10; idx++ {
    go func() {
      fmt.Println(idx)
    }()
  }
}