Infrastructure Go daemon
SECTION 6.1

Goroutines: Lightweight Threads

A goroutine is a function running concurrently. That's it.

go func() {
    fmt.Println("running in a goroutine")
}()

The go keyword launches the function on a separate goroutine. The main goroutine continues immediately.

Cost

Each goroutine starts with ~2-4KB stack (grows as needed). You can run thousands — even millions — without breaking a sweat. OS threads cost ~1MB each. This is why Go concurrency scales so well for infra tools: checking 10,000 endpoints in parallel is trivial.

Python comparison
# Python: threading.Thread is an OS thread. GIL prevents true parallelism.
# Python: asyncio is single-threaded, cooperative multitasking.
# Go: goroutines are M:N scheduled — many goroutines on few OS threads.
# Go: true parallelism on multi-core. No GIL equivalent.

The Main Goroutine Problem

2 · Worked example — read every step
func main() {
    go doWork()
    // main exits immediately — doWork never finishes!
}

main() doesn't wait for goroutines. If main returns, everything stops. You need synchronization.

3 · Fill the gaps
Launch one restart per pod without blocking the loop — and give each goroutine its own copy of the name.
for _, pod := range pods {
     func(p string) {
        fmt.Println("restarting", p)
    }()
}
// still need to wait before main returns — that's the next section
4 · From scratch — this feeds your review queue