Coding/Learn/go/go channels and communication
Go Foundational Course
01.Introduction, Toolchain & Package main
02.Variables, Zero Values, Constants & iota
03.Control Flow: If with Init Statements & Switch
04.The Unified For Loop & Range Iteration
05.Functions, Multiple Return Values & Named Returns
06.Arrays vs Slices: Dynamic Memory & Headers
07.Maps: Key-Value Hash Tables & Safe Access
08.Pointers & Value vs Pointer Semantics
09.Structs, Receiver Methods & Composition
10.Implicit Interfaces & Duck Typing with Typesafety
11.Explicit Error Handling, Wrapping & Inspection
12.Defer, Panic & Recover: Resource Cleanup
13.Strings, Runes & UTF-8 Encoding Internals
14.Goroutines & The M:N Work-Stealing Scheduler
15.Channels: CSP Concurrency & Pipelines
16.Select Statement, Non-Blocking IO & Timeouts
17.Mutexes, RWMutex & Atomic Primitives
18.Context Propagation, Cancellation & Deadlines
19.JSON Serialization, Tags & net/http Routing
20.Capstone: High-Performance Concurrent Microservice Engine
Course Certificate0%

Complete all lessons to earn

LearnChannels: CSP Concurrency & Pipelines
15 / 20 0%
Lesson 15 of 20
16 mins read

Channels: CSP Concurrency & Pipelines

PreviousNext Lesson
In Plain English

Go follows Hoare Communicating Sequential Processes (CSP). Channels provide typed synchronization conduits between goroutines. Master unbuffered vs buffered channels, channel closing, and pipeline streaming.

The Mental Model

"An unbuffered channel is handing a baton in a relay race—both runners must meet at the exact same instant. A buffered channel is a mail drop box with fixed slots."

Deep Dive: How It Works

CSP Philosophy: "Do not communicate by sharing memory; instead, share memory by communicating."

Unbuffered Channels: Synchronous rendezvous where send blocks until receiver is ready and vice versa.

Buffered Channels: Non-blocking until internal buffer capacity is full.

Range over Channels: for v := range ch drains values until sender calls close(ch).

Syntax Blueprint

ch := make(chan int, 2)
ch <- 42
val := <-ch
close(ch)

Create buffered/unbuffered channels with make, send with <-, receive with <-, and close with close().

PointCore Rules to Remember

Point star
Synchronization Conduit: Channels transfer data and ownership safely across goroutines without manual mutexes.
Point star
Sender Closes: Only the sender should close a channel to signal that no more items will arrive.
Point star
Directional Channels: Use chan<- T (send-only) and <-chan T (receive-only) in function signatures.

Common Beginner Traps & How to Fix Them

Mistake:Sending to a closed channel: close(ch); ch <- 1.

Why it happens: Sending to or closing a closed channel triggers an immediate runtime panic.

How to fix: Ensure only the producer closes the channel once.

Pro Tip: Use unbuffered channels when you need immediate synchronization guarantees, and buffered channels when decoupling producer/consumer bursts.

Live Interactive Example

Hit Run Code to see it live
Producer-Consumer Number Pipeline
Go 1.22 / Runtime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Output Console
Click "Run Code" to view the rendered output.
How it works: for num := range ch received each integer until produce called close(ch).

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Sum Numbers through a Channel

  • PointWrite a function sumWorker(nums []int, out chan<- int) that calculates the sum of a slice and sends it to out.
  • PointIn main(), launch sumWorker on slice [10, 20, 30] in a goroutine.
  • PointReceive the sum from the channel and print "Total: %d".
1
2
3
4
5
6
7
8
9
10
11
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates