By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Every program so far has done one thing at a time. It read a line, then computed, then printed, in order. Concurrency is the idea that a program can have several pieces of work in progress at the same time. In Go you do not create threads or manage thread pools. You put the word go in front of a function call.
This matters for Penny. When the API serves two requests, one should not wait for the other to finish. Each request runs in its own goroutine. The small examples below build up to that.
A goroutine is a function running alongside the code that started it. You start one by putting go in front of a call. The go statement returns immediately; the function keeps running in the background. This program starts one worker and prints only after the worker has finished:
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("worker: started")
fmt.Println("worker: finished")
}()
wg.Wait()
fmt.Println("main: worker finished, safe to exit")
}The go statement returns at once. If main reaches its closing brace, the program exits, and every goroutine that has not finished is gone. There is no warning and no error message.
// This fragment is the shape that fails: no package line, no imports and
// no WaitGroup. main returns before the worker gets a chance to print.
func main() {
go func() {
fmt.Println("worker: finished")
}()
// main returns here, so the program ends immediately.
}Run a program shaped like that and the output is often empty. That is not a bug in the goroutine. The program ended before the goroutine had anywhere to print. Any program that starts a goroutine has to keep running until the work matters.
A sync.WaitGroup is a counter with one job: block main until a number of workers have finished. The diagram shows the handoff from main, into a goroutine, and back through Done:
You use the same three calls every time:
| Call | Plain English |
|---|---|
wg.Add(1) | One more worker to wait for. Call it before starting the goroutine. |
defer wg.Done() | This worker is finished; subtract one. defer runs it when the function returns. |
wg.Wait() | Block here until the counter reaches zero. |
Every goroutine you wait for follows the same pattern, in the same order:
Call wg.Add(1) before the go statement. The counter must be raised before the worker starts.
Start the worker with go func() { ... }() or go worker().
Put defer wg.Done() as the first line inside the worker, so it runs even when the worker returns early.
Call wg.Wait() in main at the point where the results are needed.
Several workers that print inside their own function produce lines in arbitrary order, and two prints can even interleave in the middle of a line. The reliable pattern is to collect results, wait for every worker, sort, and print once from main. This program runs three workers and prints a stable list:
package main
import (
"fmt"
"sort"
"sync"
)
func main() {
var wg sync.WaitGroup
var mu sync.Mutex
results := make([]int, 0, 3)
for n := 1; n <= 3; n++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
mu.Lock()
results = append(results, n*n)
mu.Unlock()
}(n)
}
wg.Wait()
sort.Ints(results)
fmt.Println(results)
}Two details make that program safe. The mutex lets only one worker at a time run the lines between Lock and Unlock, so the shared slice is never changed in two places at once. The sort.Ints call happens after wg.Wait(), when no worker is running, so the output does not depend on which worker finished first.
# Run the program and report any data race it sees.
go run -race .
# Run every test with the same detector.
go test -race ./...Copy the results program into main.go in the penny folder. Add a fourth worker that appends 4*4. Run the program five times: the output should be [1 4 9 16] every time. Then delete the mutex lines, run go run -race . and read the warning. Put the mutex back and confirm the warning is gone.
Key takeaways
go f() starts a goroutine: a function running alongside the caller, without waiting for it.
When main returns, the program exits and unfinished goroutines are gone. Use sync.WaitGroup to wait.
The WaitGroup pattern is Add before the go statement, defer Done inside the worker, Wait in main.
Protect shared data with a mutex, collect results, sort, and print once from main for stable output.
go run -race . and go test -race ./... report data races that ordinary runs hide.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.