1
0
Fork 0
Learning-GoLang/multi-threading/basic-multi-threaded.go

26 lines
498 B
Go
Raw Normal View History

package main
import (
"fmt"
"time"
)
// Function that prints numbers from 1 to 5 with a delay
func printNumbers(id int) {
for i := 1; i <= 5; i++ {
fmt.Printf("Goroutine %d: %d\n", id, i)
time.Sleep(100 * time.Millisecond) // Delay for visibility
}
}
func main() {
// Launch 3 goroutines
for i := 1; i <= 3; i++ {
go printNumbers(i)
}
// Wait for goroutines to finish
time.Sleep(1 * time.Second) // Ensure main does not exit immediately
fmt.Println("All goroutines finished")
}