Sobes.tech
Junior — Senior

What text will appear in the console when running program №67

livecode

Task condition

Question: What output will be obtained after executing the following Go program? Pay attention to the buffered channel operation, read operations, channel closing, and the select statement.

import (
    "fmt"
)

func main() {
    soundQueue := make(chan int, 1000)

    soundQueue <- 1
    soundQueue <- 2
    soundQueue <- 3
    soundQueue <- 4
    soundQueue <- 5

    value1, present1 := <-soundQueue
    fmt.Println("%v %v \n", value1, present1)

    close(soundQueue)

    value2, present2 := <-soundQueue
    fmt.Printf("%v %v \n", value2, present2)

    select {
    case _, ok := <-soundQueue:
        if ok {
            fmt.Println("close by ok 1")
            close(soundQueue)
        }
    case _, ok := <-soundQueue:
        if ok {
            fmt.Println("close by ok 2")
            close(soundQueue)
        }
    default:
        fmt.Println("close by Default")
        close(soundQueue)
    }
}