1
0
Fork 0

Add: very simple example of a multi-threaded program

This commit is contained in:
Aroy-Art 2024-07-17 23:20:39 +02:00
parent 4c1e355303
commit 6905e99be2
Signed by: Aroy
GPG key ID: 583642324A1D2070

25
basic-multi-threaded.go Normal file
View file

@ -0,0 +1,25 @@
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")
}