Problem Statement
At Uber, we need to fetch data from multiple microservices concurrently. If any service fails, we should cancel all other requests and return the first error. Implement the errgroup package functionality from scratch.
Requirements
- Run multiple goroutines concurrently
- If any goroutine returns an error, cancel all others
- Wait for all goroutines to complete
- Return the first error encountered
Implementation
Usage Example
Edge Cases to Handle
- Multiple goroutines returning errors simultaneously
- Panic in a goroutine (add recover)
- Context already cancelled before Go() is called
Follow-up Questions
- How would you add a limit to concurrent goroutines?
- How would you collect all errors instead of just the first?
- How does the real
golang.org/x/sync/errgrouphandle panics?