Understand the contract
A channel carries values of a declared type between goroutines. An unbuffered send waits for a matching receive. A buffered channel can accept values until its capacity is full. Receives wait when no value is available unless the channel is closed and drained. These blocking rules are part of the design.
Use directional channel parameters to express ownership: chan<- T permits sending and <-chan T permits receiving. The producer in the example can send but cannot receive through its parameter. Channel operations synchronize communication; they do not make arbitrary objects passed through a channel immune to later concurrent mutation.
Work through the example
The producer sends one string and main receives it. The channel’s capacity of one allows the send to complete before main is ready. A buffer of zero would also work because the producer runs concurrently. Moving the send into main before the receive would deadlock with an unbuffered channel.
package main
import "fmt"
func produce(out chan<- string) { out <- "ready" }
func main() {
messages := make(chan string, 1)
go produce(messages)
fmt.Println(<-messages)
}Watch this boundary
len(ch) is only an observation of currently queued elements, not a reliable coordination decision. Another goroutine may act immediately afterward. Use a blocking operation or select to express the decision atomically, and do not use channel capacity as a substitute for a bounded worker-lifecycle design.
Practice with a changed input
Change the capacity from one to zero and explain why the program still finishes. Then move the send into main and identify the missing receiver before attempting to run that variant.
Lesson 44 of 50
View the full Go basics seriesReferences & further reading
Need help with your Go backend?
I work with engineering teams on focused Go and Kubernetes platform engagements, with limited consulting availability.
Explore Go consulting services