From 6905e99be2cf5bdd9e452d1f784b236b62711e11 Mon Sep 17 00:00:00 2001 From: Aroy-Art Date: Wed, 17 Jul 2024 23:20:39 +0200 Subject: [PATCH] Add: very simple example of a multi-threaded program --- basic-multi-threaded.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 basic-multi-threaded.go diff --git a/basic-multi-threaded.go b/basic-multi-threaded.go new file mode 100644 index 0000000..d7fb3f4 --- /dev/null +++ b/basic-multi-threaded.go @@ -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") +}