package main import ( "fmt" "time" ) // FutureResult holds either a value or an error. type FutureResult struct { Value interface{} Err error } // AsyncOperation simulates a long-running task and returns a channel // that will eventually provide the result. func AsyncOperation(taskID int, delay time.Duration, shouldFail bool) <-chan FutureResult { resultCh := make(chan FutureResult, 1) // Buffered channel for single result go func() { fmt.Printf("Task %d: Starting asynchronous work... ", taskID) time.Sleep(delay) if shouldFail { resultCh <- FutureResult{Err: fmt.Errorf("task %d failed after %v", taskID, delay)} } else { resultCh <- FutureResult{Value: fmt.Sprintf("Data from task %d (completed in %v)", taskID, delay)} } close(resultCh) // Important: close the channel when done sending }() return resultCh } func main() { fmt.Println("Main: Kicking off asynchronous operations.") // Start several async operations future1 := AsyncOperation(1, 2*time.Second, false) future2 := AsyncOperation(2, 1*time.Second, true) future3 := AsyncOperation(3, 3*time.Second, false) // In a real web server, you might continue handling other requests here // while these operations run in the background. fmt.Println("Main: Continuing with other work while tasks run...") time.Sleep(500 * time.Millisecond) fmt.Println("Main: Some other work done, now waiting for results.") // Wait for and process results using a select statement for i := 0; i < 3; i++ { select { case res := <-future1: if res.Err != nil { fmt.Printf("Future 1 Error: %v ", res.Err) } else { fmt.Printf("Future 1 Success: %v ", res.Value) } future1 = nil // Mark as processed to avoid re-selecting case res := <-future2: if res.Err != nil { fmt.Printf("Future 2 Error: %v ", res.Err) } else { fmt.Printf("Future 2 Success: %v ", res.Value) } future2 = nil // Mark as processed case res := <-future3: if res.Err != nil { fmt.Printf("Future 3 Error: %v ", res.Err) } else { fmt.Printf("Future 3 Success: %v ", res.Value) } future3 = nil // Mark as processed } } fmt.Println("Main: All asynchronous results processed.") }