Unbuffered Channels
Synchronous handoff between goroutines
Unbuffered Channels is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Channel?
A channel is a typed conduit for communicating between goroutines. Channels follow Go's mantra: Do not communicate by sharing memory; share memory by communicating.
package main
import "fmt"
func main() {
ch := make(chan int) // unbuffered int channel
go func() {
ch <- 42 // send: blocks until someone receives
}()
v := <-ch // receive: blocks until someone sends
fmt.Println(v) // 42
}Unbuffered Channel Semantics
An unbuffered channel has no capacity. A send blocks until a receiver is ready, and a receive blocks until a sender sends. This provides a guaranteed synchronous handoff:
package main
import "fmt"
func double(in <-chan int, out chan<- int) {
for v := range in {
out <- v * 2
}
close(out)
}
func main() {
in := make(chan int)
out := make(chan int)
go double(in, out)
go func() { in <- 3; in <- 5; close(in) }()
for v := range out { fmt.Println(v) }
// 6
// 10
}Channel as Synchronization
Unbuffered channels synchronize goroutines — use as a signal that work is done:
package main
import "fmt"
func compute(result chan<- int) {
sum := 0
for i := 1; i <= 100; i++ { sum += i }
result <- sum
}
func main() {
ch := make(chan int)
go compute(ch)
fmt.Println("Result:", <-ch) // blocks until compute sends
}Closing a Channel
The sender closes a channel with close(ch) to signal no more values will be sent. Receivers detect closure via the two-value form:
package main
import "fmt"
func generate(ch chan<- int, max int) {
for i := 0; i < max; i++ {
ch <- i
}
close(ch) // signal done
}
func main() {
ch := make(chan int)
go generate(ch, 5)
for v := range ch { // range detects close automatically
fmt.Println(v)
}
fmt.Println("channel closed")
}Two-Value Receive
The two-value receive v, ok := <-ch tells you if the channel is still open:
package main
import "fmt"
func main() {
ch := make(chan string, 1)
ch <- "hello"
close(ch)
v1, ok1 := <-ch
fmt.Println(v1, ok1) // hello true
v2, ok2 := <-ch
fmt.Println(v2, ok2) // "" false — channel closed
}Sending on a Closed Channel Panics
Sending to a closed channel panics. Only the sender should close a channel — never the receiver:
package main
import "fmt"
func main() {
ch := make(chan int)
close(ch)
// Receiving from closed channel returns zero value
v, ok := <-ch
fmt.Println(v, ok) // 0 false
// Sending to closed channel panics:
defer func() { recover() }()
ch <- 1 // panic: send on closed channel
}Directional Channels
Restrict channels to send-only (chan<- T) or receive-only (<-chan T) in function signatures for clarity and safety:
package main
import "fmt"
func producer(out chan<- int) { // send-only
for i := 0; i < 3; i++ { out <- i }
close(out)
}
func consumer(in <-chan int) { // receive-only
for v := range in { fmt.Println(v) }
}
func main() {
ch := make(chan int)
go producer(ch)
consumer(ch)
}Goroutine-Channel Ping-Pong
Two goroutines passing a message back and forth via channels — classic synchronization demo:
package main
import "fmt"
func ping(ping, pong chan struct{}) {
for i := 0; i < 3; i++ {
<-ping
fmt.Println("ping")
pong <- struct{}{}
}
}
func main() {
ping := make(chan struct{})
pong := make(chan struct{})
go func() { for { <-pong; fmt.Println("pong"); ping <- struct{}{} } }()
go func() { for i := 0; i < 3; i++ { ping <- struct{}{}; <-pong } }()
// Simplified version:
fmt.Println("channels enable structured communication")
}Nil Channels Block Forever
Receiving from or sending to a nil channel blocks forever. This is sometimes used intentionally in select statements:
package main
import "fmt"
func main() {
var ch chan int // nil channel
fmt.Println(ch == nil) // true
// select with nil channel — that case is never selected
active := make(chan int, 1)
active <- 42
select {
case v := <-ch: // never selected (nil)
fmt.Println(v)
case v := <-active: // selected
fmt.Println("got:", v)
}
}When to Use Unbuffered Channels
Use unbuffered channels when:
- You need guaranteed synchronous handoff
- The goroutine must acknowledge receipt before the sender continues
- Implementing semaphores or back-pressure
- Pipelines where each stage should process one item at a time
Quick Check
What happens when you send on an unbuffered channel with no receiver ready?
Recap: Unbuffered Channels
Summary:
- Create with
make(chan T)— zero capacity - Send blocks until receiver ready; receive blocks until sender sends
- Only the sender should close a channel
range chiterates until closed- Two-value receive:
v, ok := <-ch - Use directional types for clarity:
chan<-and<-chan
Frequently asked questions
Is the “Unbuffered Channels” lesson free?
Yes — the full text of “Unbuffered Channels” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Unbuffered Channels”?
Synchronous handoff between goroutines You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Unbuffered Channels” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Launching Goroutines
- Unbuffered Channels
- Buffered Channels
- Channel Direction and Pipeline Patterns