Skip to Content
Learning Functional Programming in Go
book

Learning Functional Programming in Go

by Lex Sheehan
November 2017
Intermediate to advanced
670 pages
17h 35m
English
Packt Publishing
Content preview from Learning Functional Programming in Go

The GenerateCars function

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Functional Programming

Learning Functional Programming

Jack Widman

Publisher Resources

ISBN: 9781787281394Supplemental Content