package main
import (
"fmt"
"sync"
"time"
)
func main() {
var wg sync.WaitGroup
urls := []string{"a", "b", "c", "d"}
for _, u := range urls {
wg.Add(1) // increment BEFORE go
go func(url string) {
defer wg.Done() // decrement when goroutine exits
time.Sleep(100 * time.Millisecond)
fmt.Println("fetched", url)
}(u) // pass `u` as arg — avoid closure capture bug
}
wg.Wait() // blocks until all Done() called
fmt.Println("all done")
}
Create a free account and build your private vault. Share publicly whenever you want.