Go · Stage 10 – Concurrency · Lesson 58 of 64

Channels

Send a typed value safely between goroutines.

Go code

messages := make(chan string)
go func() { messages <- "done" }()
message := <-messages
fmt.Println(message)
Line-by-line explanation
  1. Make creates a string channel.
  2. A goroutine sends done with the left-pointing arrow.
  3. Main receives from the channel.
  4. The message is displayed.
Expected output
done

This is the expected result.

An unbuffered send waits until another goroutine is ready to receive. That waiting provides synchronization.