November 2017
Intermediate to advanced
670 pages
17h 35m
English
Here's the actual generator function:
func (cars Collection) GenerateCars(start, limit int) Collection { carChannel := make(chan *IndexedCar)
The GenerateCars is another method in the cars collection that makes it easy to compose data transformations with other HOFs. GenerateCars takes a start index and limit, which is the number of cars that we want to be returned. We create carChannel of pointers to IndexedCars:
var waitGroup sync.WaitGroup
We use sync.WaitGroup as a counting semaphore to wait for our collection of Goroutines to finish:
numCarsToGenerate := start + limit - 1generatedCars := Collection{}waitGroup.Add(numCarsToGenerate)
We calculate the number of cars we want to generate and pass that number to the ...