golang gorutine
package main
import (
"fmt"
"sync"
"time"
)
func main() {
var wg sync.WaitGroup
// Step 1: Launching a goroutine
wg.Add(1)
go printNumbers(&wg)
// Step 2: Waiting for the goroutine to finish
wg.Wait()
}
// Step 3: Goroutine function
func printNumbers(wg *sync.WaitGroup) {
// Step 4: Defer the WaitGroup's Done method to signal completion
defer wg.Done()
// Step 5: Printing numbers in a loop
for i := 1; i <= 5; i++ {
fmt.Printf("%d ", i)
time.Sleep(time.Millisecond * 500)
}
}