ProgramGuru

Unbuffered Channels

Start with Unbuffered Channels

In this lesson, explore unbuffered channels through one small Go program, and connect its output to the language rules behind it.

What you’ll learn

  • Explain the main purpose of unbuffered channels.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

make(chan T) creates an unbuffered channel. A send completes only when a receiver can take the value; a receive waits until a sender provides one.

This rendezvous is often used to hand off work exactly when another goroutine is ready.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for unbuffered channelsAn unbuffered send and receive meet at the same handoff.Sender readyStep 1Receiver readyStep 2Value handoffStep 3Both proceedStep 4An unbuffered send and receive meet at the same handoff.
An unbuffered send and receive meet at the same handoff.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

func main() {
	handoff := make(chan int)
	go func() { handoff <- 7 }()
	n := <-handoff
	fmt.Println("Received:", n)
}

Output

Received: 7

Trace what the program does

Send and receive synchronize at the handoff. The receiver obtains 7 and prints it.

Avoid common mistakes

  • Sending to an unbuffered channel with no concurrent receiver can deadlock.
  • Unbuffered does not mean faster; it defines synchronization behavior.

Try it yourself

Add a second handoff of 8 and explain when the sender can proceed.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

When does an unbuffered channel send complete?
Choose one answer. You can try again.

Answer and explanation

When a receiver accepts the value

Correct. Send and receive synchronize at the handoff.

What you should remember

You have practiced Unbuffered Channels in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Buffered Channels →