Exercise: Deduce the Outputs
Explore concurrency concepts by deducing outputs from exercises involving goroutines and channels. This lesson helps strengthen your grasp of parallelism and synchronization in Go, preparing you for advanced topics like networking and web applications.
We'll cover the following...
We'll cover the following...
Choose one correct answer.
1.
Deduce the output of the following program
package main
import (
"fmt"
)
func tel(ch chan int) {
for i := 0; i < 5; i++ {
ch <- i
}
}
func main() {
ok := true
ch := make(chan int)
go tel(ch)
for ok {
i, ok := <-ch
fmt.Printf("ok is %t and the counter is at %d\n", ok, i)
}
}
A.
ok is true and the counter is at 0
ok is true and the counter is at 1
ok is true and the counter is at 2
ok is true and the counter is at 3
ok is true and the counter is at 4
B.
fatal error: all goroutines are asleep - deadlock!
C.
Both of A and B
D.
None of the above
1 / 5
We hope that you performed well in ...