Recipe 47 | Converting Sequential Code to Parallel |
Task
You’re working on an internal application. Your current focus is on performance and stability.
Lucy, the performance team lead, sends you a link to the following issue:
The application currently holds a list of servers it can query. When it needs to, it picks one at random and then calls its API. We started ranking the servers by access speed, but this code is sequential and takes its time. Please convert the code to concurrent.
After some thinking, you decide to start with a simple approach and use a goroutine per server.
Solution
You start by examining the current code. It defines an Info struct:
| // Info is information on site. |
| type Info struct ... |
Get Effective Go Recipes now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.