0
0
Goprogramming~5 mins

Sending and receiving values in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a channel in Go?
A channel is a way to send and receive values between goroutines safely and synchronously.
Click to reveal answer
beginner
How do you send a value to a channel in Go?
Use the syntax channel <- value to send a value into the channel.
Click to reveal answer
beginner
How do you receive a value from a channel in Go?
Use the syntax value := <-channel to receive a value from the channel.
Click to reveal answer
intermediate
What happens if you try to receive from an empty channel?
The receiving goroutine waits (blocks) until a value is sent to the channel.
Click to reveal answer
intermediate
What happens if you try to send to a full buffered channel?
The sending goroutine waits (blocks) until there is space available in the channel buffer.
Click to reveal answer
How do you send the integer 5 to a channel named ch in Go?
Asend(ch, 5)
B5 <- ch
Cch <- 5
Dch.send(5)
What does the expression value := <-ch do?
AReceives a value from channel ch and stores it in value
BSends value to channel ch
CCloses the channel ch
DCreates a new channel ch
What happens if you send to an unbuffered channel but no goroutine is ready to receive?
AThe send operation fails immediately
BThe send operation blocks until a receiver is ready
CThe value is lost
DThe channel buffers the value automatically
Which of these is true about buffered channels?
AThey never block on send
BThey block on send only when the buffer is full
CThey block on receive only when the buffer is empty
DBoth B and D
How do you declare a channel of integers in Go?
Avar ch chan int
Bvar ch int chan
Cchan int ch
Dint chan ch
Explain how sending and receiving values on channels works in Go.
Think about how goroutines communicate safely using channels.
You got /4 concepts.
    Describe what happens when you send to or receive from a channel that is not ready.
    Consider what happens if no goroutine is ready to receive or send.
    You got /3 concepts.