To receive a message from a channel in go, you can use the <-
operator with the channel variable. This operator will block the current goroutine until a message is available in the channel. Here is an example:
main.go260 chars16 lines
In this example, we create a channel of type string
using the make
function. Then, we start a new goroutine that sends a message "Hello, World!"
to the channel. Finally, we receive the message from the channel using the <-
operator and print it out.
Note that if there are no messages available in the channel, the <-
operator will block until a message is sent to the channel by another goroutine.
gistlibby LogSnag