Last active
March 23, 2025 06:11
-
-
Save gigenthomas/670cf084e176f0603c2e227122747528 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
// worker function that runs in a separate goroutine | |
func worker(id int, ch chan string) { | |
time.Sleep(time.Second) // Simulate some work | |
ch <- fmt.Sprintf("Worker %d finished work", id) | |
} | |
func main() { | |
// Create a channel to communicate between goroutines | |
ch := make(chan string) | |
// Launch multiple workers concurrently | |
for i := 1; i <= 5; i++ { | |
go worker(i, ch) | |
} | |
// Receive messages from workers | |
for i := 1; i <= 5; i++ { | |
fmt.Println(<-ch) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment