Sitemap
The 60-Second Programmer

Quick, actionable programming tips and insights delivered in just 60 seconds. Perfect for developers who want to learn on the go. Whether you’re a beginner or seasoned pro, our bite-sized lessons fit your schedule and boost your coding skills fast!

Goroutines Explained 🚀

Concurrency that costs almost nothing to start.

1 min read5 days ago

--

Want to run something concurrently in Go? Put go in front of it. Done.

What is a goroutine?

A goroutine is a lightweight thread managed by the Go runtime. They start with a tiny stack (a few KB) and grow as needed, so you can spawn thousands without breaking a sweat. The scheduler multiplexes them onto OS threads for you.

Starting one

package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
for i := 0; i

Watch out

main returning kills every goroutine, so coordinate with sync.WaitGroup or a channel. Goroutines run independently. Use channels or mutexes to share data safely. Capture loop variables explicitly (pass i as an argument) unless you're on Go 1.22+.

Get Marcelo Domingues’s stories in your inbox

Join Medium for free to get updates from this writer.

TL;DR: go f() launches concurrent work; use WaitGroup or channels to wait for it.

Try spawning 1,000 goroutines and watch how fast they finish. 🐹

--

--

Marcelo Domingues

Written by Marcelo Domingues

🚀 Lead Software Engineer for Siemens | Crafting Code & Words | Empowering Tech Enthusiasts ✨ 📲 LinkedIn: https://www.linkedin.com/in/marcelogdomingues/

The 60-Second Programmer

Published in The 60-Second Programmer

Quick, actionable programming tips and insights delivered in just 60 seconds. Perfect for developers who want to learn on the go. Whether you’re a beginner or seasoned pro, our bite-sized lessons fit your schedule and boost your coding skills fast!