Go goroutines let your program do many tasks at the same time, like downloading files while it serves users. In this post, you will learn how to start them, how to wait for them and how to avoid the common traps. Each idea comes with a short example that you can run yourself.
A goroutine runs a function, so make sure you are comfortable with writing and calling functions in Go. We will also use loops from loops in Go with range, break and continue to start many workers at once.
What Are Go Goroutines?
A goroutine is a function that runs alongside the rest of your program. The Go runtime, which is the engine inside every Go program, schedules them for you. As a result, you can start thousands of them with almost no effort.
Think of a restaurant kitchen. One chef could cook every dish in turn, but that is slow. Instead, several cooks work on different dishes at once. Go goroutines are those extra cooks.
Concurrency vs Parallelism
Concurrency means dealing with many tasks at once, by switching between them. Parallelism means doing many tasks at the exact same moment on different CPU cores. Go gives you concurrency, and it also uses parallelism automatically when your machine has several cores.
For example, a web server waits on many slow network calls at once. With Go goroutines, each request gets its own task, so one slow user never blocks the others. That is a big reason backend teams pick Go for APIs.
Starting Go Goroutines with go
func say(msg string) {
for i := 1; i <= 3; i++ {
fmt.Println(msg, i)
time.Sleep(10 * time.Millisecond)
}
}
func main() {
go say("goroutine")
say("main")
}
// main 1
// goroutine 1
// goroutine 2
// main 2
// ... (the order changes on each run)
Just put the keyword go before a function call. That call now runs in the background, while main carries on with the next line. Because both run together, their output mixes in a different order each time.
Why main Must Wait
func main() {
go fmt.Println("hi")
}
// usually prints nothing
When main returns, the whole program stops, even if goroutines are still working. So here the program usually ends before “hi” appears. Never use time.Sleep to fix this, because you can never guess the right delay.
Waiting for Go Goroutines with WaitGroup
var wg sync.WaitGroup
for i := 1; i <= 3; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
fmt.Println("worker", id, "done")
}(i)
}
wg.Wait()
fmt.Println("all workers finished")
// worker 3 done
// worker 1 done
// worker 2 done
// all workers finished
A sync.WaitGroup is a counter of running tasks. First, Add(1) raises it before each goroutine starts. Then each goroutine calls Done() when it finishes, and Wait() blocks until the counter reaches zero.

Main starts three workers, and wg.Wait() holds main until every worker calls Done().
Passing Values into a Goroutine
In the example above, i goes in as the argument id. Since Go 1.22, each loop round gets its own i, so reading it directly is also safe. Still, passing values as arguments makes your intent very clear.
You can also start a goroutine with a named function, like go sendEmail(user). Either way, the go keyword returns at once, so the caller never waits for a result. To get a result back, you need a channel, which is the topic of the next lesson.
How Cheap Are Go Goroutines?
| Feature | OS thread | Goroutine |
|---|---|---|
| Starting memory | About 1 MB or more | A few KB |
| Who schedules it | The operating system | The Go runtime |
| How many you can run | Thousands | Hundreds of thousands or more |
A goroutine starts with a tiny stack that grows only when needed. That is why a Go web server can handle each request in its own goroutine without trouble.
Common Mistakes with Go Goroutines
| Problem you see | Why it happens | How to fix it |
|---|---|---|
| Output is missing | Main returned before the goroutines finished. | Use a WaitGroup. |
| panic: sync: negative WaitGroup counter | Done ran more times than Add. | Call Add(1) once per goroutine. |
| Program hangs forever | A goroutine never called Done. | Use defer wg.Done(). |
Also, never call wg.Add inside the goroutine itself, because Wait might run first. Goroutine questions are interview favourites, so practise with my Go interview questions and answers.
Official Resources
These pages come straight from the Go team, so they are always accurate and up to date.
Quick Recap
- Go goroutines run functions alongside the rest of your program.
- Start one by writing go before a function call.
- The program stops when main returns, so always wait for your goroutines.
- Use Add, Done and Wait from sync.WaitGroup to wait properly.
- Goroutines are cheap, so you can run thousands of them.
What’s Next?
Goroutines can run together, but they still need to talk. Next, we will send data between them safely using channels.
Coming soon: Go Channels Explained with Examples


