Exercise: Deduce the Outputs

In this lesson your concepts will be evaluated through a quiz.

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

Question 1 of 50 attempted

Get hands-on with 1200+ tech skills courses.