typestar

Unbuffered channels in Go

An unbuffered send blocks until another goroutine receives: that is the handshake.

func handshake() string {
    done := make(chan string)

    go func() {
        done <- "worker finished"
    }()

    message := <-done // blocks until the send happens
    return message
}

How it works

  1. Send and receive rendezvous; neither proceeds alone.
  2. A send with no receiver deadlocks, which the runtime detects.
  3. That synchronization is the point, not a limitation.

Keywords and builtins used here

The run, in numbers

Lines
10
Characters to type
163
Tokens
32
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 19 seconds.

Type this snippet

Step 1 of 4 in Channels, step 4 of 25 in Concurrency.

← Previous Next →