Senior
// Question 5. What will be displayed on the screen func main() { ch := make(chan int, 5) for i := 1; i <= 5; i++ { ch <- i } for true { fmt.Println(<-ch) } }
sobes.tech AI
Answer from AI
This code creates a buffered channel ch with a buffer size of 5 and writes numbers from 1 to 5 into it. Then, an infinite loop reads from the channel and outputs the values.
Since the channel is filled exactly with 5 elements, the first 5 reads from the channel will output the numbers 1, 2, 3, 4, 5.
After that, the channel will be empty, and the next <-ch operation in the infinite loop will block, waiting for new data that will not arrive, as there are no further sends in the code.
As a result, the program will output the numbers from 1 to 5, each on a new line, and then hang, waiting for data from an empty channel.
Example output:
1
2
3
4
5