Sobes.tech
Junior — Senior

Determining the output from goroutines when capturing a loop variable

livecode

Task condition

Determine which lines will be printed, considering how the loop variable is captured inside the goroutine.

package main

import (
    "fmt"
)

func main() {
    out := make(chan string, 3)

    for i := 0; i < 5; i++ {
        go func() {
            out <- fmt.Sprintf("Goroutine %d", i)
        }()
    }
}

func ReceiveFromCh(ch chan string) {
    // log job
    fmt.Println(<-ch)
}