Golang go func(): Starting Goroutines in 5 Minutes
Go's go statement for beginners, in five minutes, on a real Go program: why putting go in front of three slow calls makes the program finish in zero seconds with none of them run, what the go statement does, why main never waits for goroutines, how sync.WaitGroup (Add, Done, Wait) fixes it, what go func() { ... }() means and why the final () matters, how 100,000 goroutines finish in about a second, and how go run -race and a sync.Mutex catch and fix a data race.

Faster, and nothing ran
Put the keyword go in front of three slow calls, and your checkout drops from three seconds to zero. But nothing ran. Here's why, and how go func fixes it.

Here's the checkout. After every order, it emails a receipt, updates the stock, and prints a label, one after another.
go run .receipt emailed stock updated label printed done in 3.01s
What go really does
So what does go actually do? Write it before a function call, and Go starts a goroutine: the function runs on its own, while your next line runs right away.

So put go in front of all three, and run it. It finishes in zero seconds, and not one line of output from the three jobs. All three goroutines did start. But every Go program starts in a function called main, and main reached its last line first.
sed -n '28,30p' main.gogo emailReceipt(order) go updateStock(order) go printLabel(order)
go run .done in 0.00s
And Go's specification says what happens next: the program exits. It does not wait for other goroutines.
Picture a shop at closing time. You send three helpers out on errands, then lock the door straight away. The fix is a sign-out sheet: you lock up only when everyone has signed back in.
Make main wait
In Go, that sheet is called a WaitGroup. It lives in Go's sync package, and here we name it wg. Before starting the jobs, main calls Add with three. That means: expect three jobs. Each goroutine signs back in when it finishes, by calling Done. And main stops at Wait, until the count is back to zero. Only then does main return. But who calls Done? emailReceipt doesn't know about our WaitGroup.
So wrap each call in a small function with no name, and let that function call Done. On screen: the word go, then func, Go's word for a function, and two empty parentheses at the very end. That's go func.

The first line inside says defer wg dot Done. Defer means: run this when the function finishes, however it finishes. Then comes the real call. And those last parentheses call the function right now.
sed -n '29,34p' main.govar wg sync.WaitGroup wg.Add(3) go func() { defer wg.Done(); emailReceipt(order) }() go func() { defer wg.Done(); updateStock(order) }() go func() { defer wg.Done(); printLabel(order) }() wg.Wait()
Watch what happens if you delete them on one line. The build stops. The compiler says: expression in go must be function call. You wrote a function, and never called it. Now put them back and run it again: all three jobs, in whatever order they finish, done in one second, not three.
go run .# checkout ./main.go:31:5: expression in go must be function call
go run .stock updated label printed receipt emailed done in 1.01s
On Go one twenty-five or newer, the WaitGroup has a shortcut method, also called Go: give it your function, with no extra parentheses, and it calls Add, starts the goroutine, and calls Done for you.

Thousands at once
A real checkout doesn't get three orders, it gets thousands at once. Let's email a hundred thousand receipts, a goroutine each, every one waiting a full second. Guess first: how long does that take? About one second. They all wait at the same time. And a new goroutine starts with only a few kilobytes of memory, so a laptop holds a hundred thousand easily.
sed -n '17,22p' main.govar wg sync.WaitGroup for order := range 100000 { wg.Add(1) go func() { defer wg.Done(); emailReceipt(order) }() } wg.Wait()
go run .receipts emailed: 100000 done in 1.08s
One trap is left. Start a thousand goroutines, and have each one add one to a shared counter of receipts sent. Guess first: what's the final count? It should be one thousand. We got seven hundred sixty-seven. Run it again: eight hundred sixty-four. Two goroutines both read forty-one, both add one, and both write forty-two. Two receipts were sent, but the count only went up by one. That's a data race.
sed -n '9,18p' main.gosent := 0 var wg sync.WaitGroup for range 1000 { wg.Add(1) go func() { defer wg.Done(); sent++ }() } wg.Wait() fmt.Println("receipts sent:", sent)
go run .receipts sent: 767
go run .receipts sent: 864
Go can catch this for you. Run with dash race, and it prints: warning, data race, with the line number. The fix is a mutex, a lock, from the same sync package. Call Lock before touching the counter, and Unlock right after, so only one goroutine is in there at a time. Now it's one thousand, and the race detector stays quiet.
go run -race .================== WARNING: DATA RACE Read at 0x00c000014108 by goroutine 16: main.main.func1() /home/dev/checkout/main.go:14 +0x68 Previous write at 0x00c000014108 by goroutine 26: main.main.func1() /home/dev/checkout/main.go:14 +0x78 Goroutine 16 (running) created at: main.main() /home/dev/checkout/main.go:14 +0x6c Goroutine 26 (finished) created at: main.main() /home/dev/checkout/main.go:14 +0x6c ================== ================== WARNING: DATA RACE Write at 0x00c000014108 by goroutine 16: main.main.func1() /home/dev/checkout/main.go:14 +0x78 Previous write at 0x00c000014108 by goroutine 23: main.main.func1() /home/dev/checkout/main.go:14 +0x78 Goroutine 16 (running) created at: main.main() /home/dev/checkout/main.go:14 +0x6c Goroutine 23 (finished) created at: main.main() /home/dev/checkout/main.go:14 +0x6c ================== receipts sent: 655 Found 2 data race(s) exit status 66
sed -n '9,19p' main.gosent := 0 var mu sync.Mutex var wg sync.WaitGroup for range 1000 { wg.Add(1) go func() { defer wg.Done(); mu.Lock(); sent++; mu.Unlock() }() } wg.Wait() fmt.Println("receipts sent:", sent)
go run -race .receipts sent: 1000
Recap
So: go starts a function, and never waits. When main returns, everything stops. A WaitGroup makes main wait. Go func wraps a call, so it can call Done. And shared data needs a lock. Run with dash race to find where.

If your goroutines finally finish, subscribing keeps more Go idioms coming: one idea, one real program, five minutes at a time.
The lab and every command are linked in the description, so you can run every step on your own laptop. Thanks for watching Cloud Explainers.
Sources
Demos: one container on our own Mac standing in for your laptop; the checkout and its jobs are made up (each job just waits one second). Every command, output and number was real. Watch the full video on YouTube.