Skip to content

Instantly share code, notes, and snippets.

@gigenthomas
Last active March 23, 2025 06:11
Show Gist options
  • Save gigenthomas/670cf084e176f0603c2e227122747528 to your computer and use it in GitHub Desktop.
Save gigenthomas/670cf084e176f0603c2e227122747528 to your computer and use it in GitHub Desktop.
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