Sobes.tech
Back to tasks
Junior — Senior
29

Буферный канал, горутины и бесконечный select‑цикл

Get help with live coding in real time with Sobes Copilot
Task condition

Каким будет вывод следующей программы?

func main() {
    var wgGroup sync.WaitGroup
    bufChan := make(chan string, 3)

    for idx := 0; idx < 5; idx++ {
        wgGroup.Add(1)
        go func(ch chan<- string, i int, wg *sync.WaitGroup) {
            defer wg.Done()
            ch <- fmt.Sprintf("Result from goroutine %s", strconv.Itoa(i))
        }(bufChan, idx, &wgGroup)
    }

    for {
        select {
        case v := <-bufChan:
            fmt.Println(v)
        }
    }

    wgGroup.Wait()
    close(bufChan)
}