Goroutines and channels: work at the same time
Learn Go · Beginner · 12 min · After Generics in Go: one function, many types
How does Go run several things at once without shared-memory bugs?
1Guess
package main import "fmt" func main() { ch := make(chan int) go func() { for i := 1; i <= 3; i++ { ch <- i } close(ch) }() for v := range ch { fmt.Println(v) } }2Read
Goroutines
Put
gobefore a function call and it runs as a goroutine, at the same time as the rest of your code. Whenmainreturns, the program ends, even if goroutines are still running. To wait for them, use async.WaitGroup: callwg.Add(1)before starting,defer wg.Done()inside the goroutine andwg.Wait()where you need all of them finished.3Code
Three workers and a WaitGroup
1 / 2package main import ( "fmt" "sync" ) func main() { results := make(chan int, 3) var wg sync.WaitGroup for i := 1; i <= 3; i++ { wg.Add(1) go func() { defer wg.Done() results <- i * i }() } wg.Wait() close(results) sum := 0 for r := range results { sum += r } fmt.Println("sum:", sum) }Read the program. What will it print?
4Read
Channels
A channel moves values between goroutines:
ch <- vsends andv := <-chreceives. On an unbuffered channel, a send waits until another goroutine receives. A buffered channel,make(chan int, 3), lets sends continue until it is full.close(ch)tells receivers no more values come, andrangestops then.selectwaits on several channels;defaultruns when none is ready.5Code
A pipeline and a select
1 / 2package main import "fmt" func generate(n int) <-chan int { out := make(chan int) go func() { for i := 1; i <= n; i++ { out <- i } close(out) }() return out } func square(in <-chan int) <-chan int { out := make(chan int) go func() { for v := range in { out <- v * v } close(out) }() return out } func main() { for v := range square(generate(4)) { fmt.Println(v) } empty := make(chan int) select { case v := <-empty: fmt.Println("got", v) default: fmt.Println("nothing ready") } }Read the program. What will it print?
6Fix
package main import "fmt" func main() { ch := make(chan int) ch <- 1 fmt.Println(<-ch) }7Say it back
8Mission · 12 min
Build a worker pool: three workers read the numbers 1 to 9 from a jobs channel and send their squares to a results channel. Add up the results and print the total, which must be 285.
go run .
Common questions
- What is a goroutine?
- A goroutine is a function running at the same time as other code in the same program. You start one by putting go before a call. Goroutines are cheap, so a program can run thousands of them. They share memory, so Go advice is to pass values over channels instead of sharing variables, which avoids many timing bugs.
- Why does my program end before the goroutine prints?
- When main returns, the whole program exits, and it does not wait for goroutines that are still running. You must make main wait. The usual tools are a sync.WaitGroup, which counts running goroutines, or a channel that main receives from. A sleep is not a fix, because it only guesses how long the work takes.
- When should I close a channel?
- Close a channel when the sender has no more values, so receivers using range or the two-value receive know to stop. Only the sender should close it, and never more than once, or the program panics. You do not have to close a channel for cleanup; the garbage collector frees it when nothing uses it.
Sources
- https://go.dev/ref/spec#Go_statements (read 2026-12-03)
- https://go.dev/ref/spec#Channel_types (read 2026-12-03)
- https://pkg.go.dev/sync#WaitGroup (read 2026-12-03)
Measured with go1.27.4 darwin/arm64 on 2026-12-03
Published Dec 3, 2026 · Updated Dec 3, 2026