From 1e0d664fdad532346e1f9eb12884c66610f27b68 Mon Sep 17 00:00:00 2001 From: Mo Date: Wed, 11 Feb 2026 18:12:37 -0500 Subject: [PATCH 1/3] Implement WorkerPool and Fibonacci examples in Go --- test.go | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 test.go diff --git a/test.go b/test.go new file mode 100644 index 00000000000..b802e8aee21 --- /dev/null +++ b/test.go @@ -0,0 +1,125 @@ +package main + +import ( + "fmt" + "math/rand" + "sync" + "time" +) + +// WorkerPool demonstrates a pool of goroutines processing jobs +type WorkerPool struct { + workerCount int + jobs chan int + results chan int + wg sync.WaitGroup +} + +// NewWorkerPool creates a new WorkerPool +func NewWorkerPool(workerCount, jobCount int) *WorkerPool { + return &WorkerPool{ + workerCount: workerCount, + jobs: make(chan int, jobCount), + results: make(chan int, jobCount), + } +} + +// Start initializes the pool and launches workers +func (wp *WorkerPool) Start() { + for i := 0; i < wp.workerCount; i++ { + wp.wg.Add(1) + go wp.worker(i) + } +} + +// AddJob adds a job to the pool +func (wp *WorkerPool) AddJob(job int) { + wp.jobs <- job +} + +// CloseJobs closes the jobs channel +func (wp *WorkerPool) CloseJobs() { + close(wp.jobs) +} + +// Wait waits for all workers to finish +func (wp *WorkerPool) Wait() { + wp.wg.Wait() + close(wp.results) +} + +// worker is the function run by each goroutine in the pool +func (wp *WorkerPool) worker(id int) { + defer wp.wg.Done() + for job := range wp.jobs { + result := process(job) + fmt.Printf("Worker %d processed job %d, result: %d\n", id, job, result) + wp.results <- result + } +} + +// process simulates a time-consuming job +func process(n int) int { + time.Sleep(time.Millisecond * time.Duration(rand.Intn(200))) + return n * n +} + +// fibonacci calculates the nth Fibonacci number recursively +func fibonacci(n int) int { + if n <= 1 { + return n + } + return fibonacci(n-1) + fibonacci(n-2) +} + +// printFibonacci prints the first n Fibonacci numbers +func printFibonacci(n int) { + fmt.Printf("Fibonacci sequence up to %d:\n", n) + for i := 0; i < n; i++ { + fmt.Printf("%d ", fibonacci(i)) + } + fmt.Println() +} + +func main() { + rand.Seed(time.Now().UnixNano()) + fmt.Println("Starting WorkerPool example...") + + // WorkerPool example + const jobCount = 10 + wp := NewWorkerPool(3, jobCount) + wp.Start() + + for i := 1; i <= jobCount; i++ { + wp.AddJob(i) + } + wp.CloseJobs() + wp.Wait() + + sum := 0 + for result := range wp.results { + sum += result + } + fmt.Printf("Sum of results: %d\n", sum) + + // Fibonacci example + printFibonacci(10) + + // Simulated HTTP server example + requests := make(chan string, 5) + responses := make(chan string, 5) + quit := make(chan struct{}) + + //go simulateHTTPServer(requests, responses, quit) + + for i := 0; i < 5; i++ { + requests <- fmt.Sprintf("request-%d", i) + } + + for i := 0; i < 5; i++ { + fmt.Println(<-responses) + } + + close(quit) + fmt.Println("All examples complete.") +} From e8af3e8db7cbc3df9670353dd8ed983e6c2315a5 Mon Sep 17 00:00:00 2001 From: Mo Date: Wed, 11 Feb 2026 18:18:13 -0500 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- test.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/test.go b/test.go index b802e8aee21..b2553073075 100644 --- a/test.go +++ b/test.go @@ -45,7 +45,7 @@ func (wp *WorkerPool) CloseJobs() { // Wait waits for all workers to finish func (wp *WorkerPool) Wait() { wp.wg.Wait() - close(wp.results) +} } // worker is the function run by each goroutine in the pool @@ -81,6 +81,26 @@ func printFibonacci(n int) { fmt.Println() } +// simulateHTTPServer simulates handling HTTP requests and producing responses. +func simulateHTTPServer(requests <-chan string, responses chan<- string, quit <-chan struct{}) { + for { + select { + case req := <-requests: + // Simulate processing the request. + time.Sleep(50 * time.Millisecond) + responses <- fmt.Sprintf("response to %s", req) + case <-quit: + // Cleanly shut down the simulated server. + close(responses) + return + } + go func() { + wp.Wait() + close(wp.results) + }() + +} + func main() { rand.Seed(time.Now().UnixNano()) fmt.Println("Starting WorkerPool example...") @@ -110,7 +130,7 @@ func main() { responses := make(chan string, 5) quit := make(chan struct{}) - //go simulateHTTPServer(requests, responses, quit) + go simulateHTTPServer(requests, responses, quit) for i := 0; i < 5; i++ { requests <- fmt.Sprintf("request-%d", i) From 3549ac8ba43381bc94c1d9b7e92872e839a6fabf Mon Sep 17 00:00:00 2001 From: Mo Date: Wed, 11 Feb 2026 18:23:14 -0500 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Mo --- test.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/test.go b/test.go index b2553073075..85285378d43 100644 --- a/test.go +++ b/test.go @@ -7,6 +7,11 @@ import ( "time" ) +var ( + rngMu sync.Mutex + rng = rand.New(rand.NewSource(time.Now().UnixNano())) +) + // WorkerPool demonstrates a pool of goroutines processing jobs type WorkerPool struct { workerCount int @@ -60,12 +65,16 @@ func (wp *WorkerPool) worker(id int) { // process simulates a time-consuming job func process(n int) int { - time.Sleep(time.Millisecond * time.Duration(rand.Intn(200))) - return n * n + rngMu.Lock() + delay := rng.Intn(200) + rngMu.Unlock() + + time.Sleep(time.Millisecond * time.Duration(delay)) +barbaz } // fibonacci calculates the nth Fibonacci number recursively -func fibonacci(n int) int { +Hi Mom if n <= 1 { return n }