0
0
Goprogramming~10 mins

Select with multiple channels in Go - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to receive a value from channel c1.

Go
select {
case msg := <-[1]:
    fmt.Println("Received", msg)
}
Drag options to blanks, or click blank then click option'
Anil
Bc1
Cdone
Dc2
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong channel name in the receive expression.
Trying to receive from a closed or nil channel.
2fill in blank
medium

Complete the code to send the string "hello" to channel c2 inside the select.

Go
select {
case [1] <- "hello":
    fmt.Println("Sent hello")
}
Drag options to blanks, or click blank then click option'
Ac2
Bc1
Cmsg
Ddone
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the value before the arrow instead of the channel.
Using a variable name instead of the channel.
3fill in blank
hard

Fix the error in the select statement to correctly receive from channel done.

Go
select {
case <-[1]:
    fmt.Println("Done signal received")
}
Drag options to blanks, or click blank then click option'
Adone
Bc2
Cc1
Dmsg
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name instead of the channel.
Trying to assign the received value when none is sent.
4fill in blank
hard

Fill both blanks to receive from c1 and send "ok" to c2 inside the select.

Go
select {
case msg := <-[1]:
    fmt.Println("Received", msg)
case [2] <- "ok":
    fmt.Println("Sent ok")
}
Drag options to blanks, or click blank then click option'
Ac1
Bc2
Cdone
Dmsg
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up send and receive channels.
Using variable names instead of channel names.
5fill in blank
hard

Fill all three blanks to receive from c1, send "done" to c2, and receive from done channel.

Go
select {
case msg := <-[1]:
    fmt.Println("Received", msg)
case [2] <- "done":
    fmt.Println("Sent done")
case <-[3]:
    fmt.Println("Done signal received")
}
Drag options to blanks, or click blank then click option'
Ac1
Bc2
Cdone
Dmsg
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing send and receive operations.
Using variable names instead of channel names.